Date Disappearing from array - javascript

So I have this problem right now. I'm writing a mobile app through react native.
The problem is I'm trying to manipulate an array and it seems that a date drops from it for no reason a some point. here is the code and the output that it produces:
const res = labelDates.map((val) => {
const date = new Date();
date.setTime(0);
date.setFullYear(val.date.getFullYear());
date.setMonth(val.date.getMonth());
date.setDate(val.date.getDate());
console.log('mapping ' + val.date + ' to ' + date);
return date;
});
console.log(res);
And here is the output
mapping Thu Sep 03 2020 16:25:36 GMT+0200 (CEST) to Thu Sep 03 2020 01:00:00 GMT+0200 (CEST)
mapping Fri Sep 04 2020 16:28:40 GMT+0200 (CEST) to Fri Sep 04 2020 01:00:00 GMT+0200 (CEST)
mapping Sat Sep 05 2020 12:48:36 GMT+0200 (CEST) to Sat Sep 05 2020 01:00:00 GMT+0200 (CEST)
mapping Sun Sep 06 2020 14:28:56 GMT+0200 (CEST) to Sun Sep 06 2020 01:00:00 GMT+0200 (CEST)
mapping Mon Sep 07 2020 11:28:16 GMT+0200 (CEST) to Mon Sep 07 2020 01:00:00 GMT+0200 (CEST)
mapping Tue Sep 08 2020 18:22:36 GMT+0200 (CEST) to Tue Sep 08 2020 01:00:00 GMT+0200 (CEST)
mapping Thu Sep 10 2020 00:18:36 GMT+0200 (CEST) to Thu Sep 10 2020 01:00:00 GMT+0200 (CEST)
mapping Fri Sep 11 2020 00:18:36 GMT+0200 (CEST) to Fri Sep 11 2020 01:00:00 GMT+0200 (CEST)
mapping Sat Sep 12 2020 00:18:36 GMT+0200 (CEST) to Sat Sep 12 2020 01:00:00 GMT+0200 (CEST)
mapping Sun Sep 13 2020 00:18:36 GMT+0200 (CEST) to Sun Sep 13 2020 01:00:00 GMT+0200 (CEST)
mapping Mon Sep 14 2020 00:18:36 GMT+0200 (CEST) to Mon Sep 14 2020 01:00:00 GMT+0200 (CEST)
mapping Tue Sep 15 2020 00:18:36 GMT+0200 (CEST) to Tue Sep 15 2020 01:00:00 GMT+0200 (CEST)
mapping Wed Sep 16 2020 00:18:36 GMT+0200 (CEST) to Wed Sep 16 2020 01:00:00 GMT+0200 (CEST)
mapping Thu Sep 17 2020 00:18:36 GMT+0200 (CEST) to Thu Sep 17 2020 01:00:00 GMT+0200 (CEST)
mapping Fri Sep 18 2020 00:18:36 GMT+0200 (CEST) to Fri Sep 18 2020 01:00:00 GMT+0200 (CEST)
mapping Sat Sep 19 2020 00:18:36 GMT+0200 (CEST) to Sat Sep 19 2020 01:00:00 GMT+0200 (CEST)
mapping Sun Sep 20 2020 00:18:36 GMT+0200 (CEST) to Sun Sep 20 2020 01:00:00 GMT+0200 (CEST)
mapping Mon Sep 21 2020 00:18:36 GMT+0200 (CEST) to Mon Sep 21 2020 01:00:00 GMT+0200 (CEST)
Array [
2020-09-02T23:00:00.000Z,
2020-09-03T23:00:00.000Z,
2020-09-04T23:00:00.000Z,
2020-09-05T23:00:00.000Z,
2020-09-06T23:00:00.000Z,
2020-09-07T23:00:00.000Z,
2020-09-09T23:00:00.000Z,
2020-09-10T23:00:00.000Z,
2020-09-11T23:00:00.000Z,
2020-09-12T23:00:00.000Z,
2020-09-13T23:00:00.000Z,
2020-09-14T23:00:00.000Z,
2020-09-15T23:00:00.000Z,
2020-09-16T23:00:00.000Z,
2020-09-17T23:00:00.000Z,
2020-09-18T23:00:00.000Z,
2020-09-19T23:00:00.000Z,
2020-09-20T23:00:00.000Z,
]
As you see during the map call the array is containing the date 2020-09-08, but right after it terminates, my res array has dropped it for no reason!
I am sure data doesn't change since it's hardcoded and there is no other thread handling it.

Related

A way generate an array of length 30 with random time between two dates using JS?

I have start and end properties in range object. And each property has time value.
range = {
start: Tue Jul 07 2020 10:58:05,
end: Wed Jul 08 2020 10:58:05
}
then I have to make an array of length 30 that contains random time between range.start and range.end.
[Tue Jul 07 2020 11:00:05, Tue Jul 07 2020 13:49:12, Tue Jul 07 2020 15:22:54... Wed Jul 08 2020 12:51:05, Wed Jul 08 2020 15:24:13]
I think I can do it using new Array(30).fill(dates) but have no clue what to put it inside dates.
This converts your dates to timestamps, then generates random numbers in-between and converts those back to dates. Note: added timezone
var start = new Date('Tue Jul 07 2020 10:58:05 GMT-0400').getTime();
var end = new Date('Wed Jul 08 2020 10:58:05 GMT-0400').getTime();
var dates = Array(30).fill().map(() => {
return new Date(
Math.floor(Math.random() * (end - start)) + start
).toString();
});
Results in:
["Wed Jul 08 2020 01:55:53 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 16:58:52 GMT-0400 (Eastern Daylight Time)"
"Wed Jul 08 2020 00:02:45 GMT-0400 (Eastern Daylight Time)"​
"Tue Jul 07 2020 15:33:55 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 20:16:20 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 15:25:33 GMT-0400 (Eastern Daylight Time)"​
"Tue Jul 07 2020 17:15:14 GMT-0400 (Eastern Daylight Time)"
"Wed Jul 08 2020 02:20:32 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 23:25:54 GMT-0400 (Eastern Daylight Time)"
...]
Edit: for unique dates you can do something like this:
var start = new Date('Tue Jul 07 2020 10:58:05 GMT-0400').getTime();
var end = new Date('Wed Jul 08 2020 10:58:05 GMT-0400').getTime();
var dates = [];
while(dates.length < 30) {
let date = new Date(
Math.floor(Math.random() * (end - start)) + start
).toString();
if (!dates.includes(date)) {
dates.push(date);
}
}
I wouldn't use this if the start and end dates are very close (within seconds of eachother) and/or the output array is very large. It will block thread.
I'm not big fan of Array(n).fill() just to create an empty array with n undefined elements as the basis for a loop when the array is then discarded.
A for loop is likely less code and more efficient, e.g. (same algorithm as GitGitBoom):
let range = {
start: new Date(2020,6,7,10,58,05),
end: new Date(2020,6,8,10,58,05)
}
let result = [];
for (let s = +range.start, diff = range.end - s, i = 30; i; --i) {
result.push(new Date(s + Math.random() * diff).toString());
}
console.log(result.sort());
If you're prepared to use var, the result array can be declared in the for loop initialiser too.
Assuming you have a randomTimeGenerator, function:
var randomTimes = [];
var i=30; while(i--){randomTimes.push(randomTimeGenerator(i, randomTimes))}

Check if Date exist in array of Object

I have an array of date i want to sort it and get only the recent Date
[
"Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)"
]
shouldDisplayDate(date: Date) {
datesFiltered = [];
const array = this.users.map(a => a.date)
for (const date of array) {
if (!this.datesFiltered.find(d => new Date(d).setHours(0, 0, 0) ===
new Date(dateString).setHours(0, 0, 0))) {
this.datesFiltered.push(new Date(dateString).toString())
}
}
}
Result :
[
Mon Jul 16 2018 15:32:50 GMT+0200 (Central European Summer Time),
Fri Jul 13 2018 09:33:46 GMT+0200 (Central European Summer Time),
Thu Jul 12 2018 13:41:59 GMT+0200 (Central European Summer Time)
]
So I want to check if I enter Mon Jul 16 2018 11:40:28 GMT+0200 (CEST) is in the Array of Object or not?
To see if two dates are equal using === you'll have to use .getTime() on them first (see this answer for more info)
An example of doing a simple date sort and finding if a date exists in your array are below.
To check if the date exists in the array I first convert a string date (the one we're searching for existence) to a Date obj. Then I loop through the array of existing dates and convert them one at a time and use Date.getTime() on each date to see if they are equal, if so, the function will return true.
const dates = [
"Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
"Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)"
];
// simple date sort
let sortedDates = dates.sort(function(a, b) {
return new Date(b) - new Date(a);
});
console.log(`sorted Dates array is: ${JSON.stringify(sortedDates, null, 2)}`);
console.log(`date exists in array? ${isDateInArray("Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)")}`);
function isDateInArray(dateString) {
let dateExists = false;
let date = new Date(dateString);
dates.forEach(function(arrayDateString) {
let arrayDate = new Date(arrayDateString);
if(date.getTime() === arrayDate.getTime()){
dateExists = true;
}
});
return dateExists;
}

How to compare two arrays containing dates to check if any values match?

I currently have two arrays containing dates. I want to check if there's at least one match of values when comparing the two arrays. To clarify, these are Date objects and not strings. My code below always seems to return false:
var between = [Sun Aug 27 2017 00:00:00 GMT+0100 (BST), Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Tue Aug 29 2017 00:00:00 GMT+0100 (BST), Wed Aug 30 2017 00:00:00 GMT+0100 (BST)];
var myDate = [Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Thu Aug 24 2017 00:00:00 GMT+0100 (BST)];
var bExists = false;
$.each(myDate, function(index, value){
if($.inArray(value,between)!=-1){
console.log(value);
bExists = true;
}
if(bExists){
return false; //break
}
});
console.log(bExists);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
In this way you can find the one's that match
Array.prototype.diff = function(arr2) {
var ret = [];
for(var i in this) {
if(arr2.indexOf( this[i] ) > -1){
ret.push( this[i] );
}
}
return ret;
};
You can make use of .some of Array to check at least one element satisfy the condition.
var between = ["Sun Aug 27 2017 00:00:00 GMT+0100 (BST)", "Mon Aug 28 2017 00:00:00 GMT+0100 (BST)", "Tue Aug 29 2017 00:00:00 GMT+0100 (BST)", "Wed Aug 30 2017 00:00:00 GMT+0100 (BST)"];
var myDate = ["Mon Aug 28 2017 00:00:00 GMT+0100 (BST)", "Thu Aug 24 2017 00:00:00 GMT+0100 (BST)"];
//checking atleast one present
var atleastOne = myDate.some(function(item){
return between.indexOf(item) > -1;
});
//checking that all present
var all = myDate.every(function(item){
return between.indexOf(item) > -1;
});
console.log(all);
var between = [Sun Aug 27 2017 00:00:00 GMT+0100 (BST), Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Tue Aug 29 2017 00:00:00 GMT+0100 (BST), Wed Aug 30 2017 00:00:00 GMT+0100 (BST)];
var myDate = [Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Thu Aug 24 2017 00:00:00 GMT+0100 (BST)];
var bExists = false;
for ( var i = 0; i < between.length; i++ ) {
for ( var e = 0; e < myDate.length; e++ ) {
if ( between[i] === myDate[e] ){
bExists = true;
}
}
console.log(bExists);
You could use Array.some()
The some() method tests whether at-least one element in the array passes the test implemented by the provided function.
var between = ['Sun Aug 27 2017 00:00:00 GMT+0100 (BST)', 'Mon Aug 28 2017 00:00:00 GMT+0100 (BST)','Tue Aug 29 2017 00:00:00 GMT+0100 (BST)', 'Wed Aug 30 2017 00:00:00 GMT+0100 (BST)'];
var myDate = ['Mon Aug 28 2017 00:00:00 GMT+0100 (BST)', 'Thu Aug 24 2017 00:00:00 GMT+0100 (BST)'];
console.log(between.some(date => myDate.includes(date)));
var between = ['Sun Aug 27 2017 00:00:00 GMT+0100 (BST)', 'Mon Aug 28 2017 00:00:00 GMT+0100 (BST)', 'Tue Aug 29 2017 00:00:00 GMT+0100 (BST)', 'Wed Aug 30 2017 00:00:00 GMT+0100 (BST)'];
var myDate = ['Mon Aug 28 2017 00:00:00 GMT+0100 (BST)', 'Thu Aug 24 2017 00:00:00 GMT+0100 (BST)'];
var isExist = false;
//checking atleast one present
$.each(myDate ,function(index , value){
if(between.indexOf(value) != "-1")
{
isExist = true;
return;
}
});
console.log(isExist);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var between = [new Date('Sun Aug 27 2017 00:00:00 GMT+0100 (BST)'), new Date('Mon Aug 28 2017 00:00:00 GMT+0100 (BST)'), new Date('Tue Aug 29 2017 00:00:00 GMT+0100 (BST)'), new Date('Wed Aug 30 2017 00:00:00 GMT+0100 (BST)')];
var myDate = [new Date('Mon Aug 28 2017 00:00:00 GMT+0100 (BST)'), new Date('Thu Aug 24 2017 00:00:00 GMT+0100 (BST)')];
var bExists = false;
$.each(myDate, function(index, myDateValue){
$.each(between, function(index, betweenValue){
if(myDateValue.toString() == betweenValue.toString()){
console.log(betweenValue);
bExists = true;
return false;
}
})
});
console.log(bExists);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Convert your Date object toString or else you can use getTime and then compare.

How to convert Sun Aug 16 2015 00:00:00 GMT+0200 (W. Europe Daylight Time) to 16.08.2015

Iam trying to convert the Sun Aug 16 2015 00:00:00 GMT+0200 (W. Europe Daylight Time) to 16.08.2015 format but not finding any food solution. Anyone have experience ??
you can try:
var d = new Date('Sun Aug 16 2015 00:00:00 GMT+0200');
console.log(d.getDate()+'/'+ ((d.getMonth()+1)< 10?'0'+(d.getMonth()+1):(d.getMonth()+1)) + '/' + d.getFullYear() ); //16/08/2015

How to convert an array of UTC dates into milliseconds using JS/Jquery

I'm having an array of dates:
var ticks = [];
ticks = [Tue Jan 01 2013 00:00:00 GMT-0800 (PST), Fri Feb 01 2013 00:00:00 GMT-0800 (PST), Fri Mar 01 2013 00:00:00 GMT-0800 (PST), Mon Apr 01 2013 00:00:00 GMT-0700 (PDT), Wed May 01 2013 00:00:00 GMT-0700 (PDT), Sat Jun 01 2013 00:00:00 GMT-0700 (PDT), Mon Jul 01 2013 00:00:00 GMT-0700 (PDT), Thu Aug 01 2013 00:00:00 GMT-0700 (PDT), Sun Sep 01 2013 00:00:00 GMT-0700 (PDT)].
How can I convert the above array of dates into millisecond format:
something like below:
[1390000000000, 1395000000000, 1400000000000, 1405000000000, 1410000000000, 1415000000000]
(PS: I need this format as I have a function that takes this format to display the x-axis)
Any ideas on how this can be achieved? Thanks!!!!
You could simply map a new array from the date objects:
ticks.map(function ( value ) {
return value.getTime();
});
Assuming you will get the raw data in string format, or else you have a bit of todo
var ticks = ... // raw data
var t2 = [];
for ( var i = 0; i < ticks.length; ++i ) {
t2.push( new Date(ticks[i]).getTime() );
}

Categories