JS setDate() in loop - javascript

I want to call a function for each day within next two weeks from now and pass parameters such as day and month. I use this method: startDate.setDate(startDate.getDate() + 1) which I found here but it gets messy after first 6 steps of the loop
var date = new Date,
searchDate = new Date,
period = 14;
for(i = 1; i <= period; i++){
searchDate.setDate(date.getDate() + i);
// someFunction(searchDate.getDate(), searchDate.getMonth());
}
What console.log(i + ": " + searchDate) returns:
1: Tue Aug 27 2013 17:38:04 GMT+0200
2: Wed Aug 28 2013 17:38:04 GMT+0200
3: Thu Aug 29 2013 17:38:04 GMT+0200
4: Fri Aug 30 2013 17:38:04 GMT+0200
5: Sat Aug 31 2013 17:38:04 GMT+0200
6: Sun Sep 01 2013 17:38:04 GMT+0200 // so far so good
7: Thu Oct 03 2013 17:38:04 GMT+0200 // and after a week it's suddenly October
8: Sun Nov 03 2013 17:38:04 GMT+0100
9: Thu Dec 05 2013 17:38:04 GMT+0100
10: Sun Jan 05 2014 17:38:04 GMT+0100
11: Thu Feb 06 2014 17:38:04 GMT+0100
12: Mon Mar 10 2014 17:38:04 GMT+0100
13: Tue Apr 08 2014 17:38:04 GMT+0200
14: Sat May 10 2014 17:38:04 GMT+0200
Any clue on this?

What you want is :
searchDate.setTime(date.getTime() + (24 * 3600 * 1000 * i));
It adds time to the internal value (timestamp) of the date instead of adding it to the day.

Related

Date Disappearing from array

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.

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;
}

Why this JavaScript date() is weird?

When I run the following JavaScript code it returns
new Date(2017, 5, 31)
// Sat Jul 01 2017 00:00:00 GMT+0530 (IST)
Here I understand months are zero based in Date() so it overflows to July. But when I run following
new Date(2017, 12, 31)
// Wed Jan 31 2018 00:00:00 GMT+0530 (IST)
Here why the date is Jan 31 instead of throwing an exception?
new Date(2017, 13, 31)
// Sat Mar 03 2018 00:00:00 GMT+0530 (IST). Why Mar 03 instead of Mar 31?
Thanks
new Date(2017, 5, 31)
// Sat Jul 01 2017 00:00:00 GMT+0530 (IST)
June has only 30 days, so the balance 1 day (31 - 30 = 1) overflow to become July 01.
new Date(2017, 12, 31)
// Wed Jan 31 2018 00:00:00 GMT+0530 (IST)
Similarly, year 2017 has only 12 months, so the balance 1 month overflow to become 2018 Jan. Coincidently, January has 31 days too, so it becomes 2018 Jan 31 (31 - 31 = 0).
new Date(2017, 13, 31)
// Sat Mar 03 2018 00:00:00 GMT+0530 (IST). Why Mar 03 instead of Mar 31?
By that logic, year 2017 has only 12 months, so the balance 2 months overflow to become 2018 February.
Unfortunately, Febraury of 2018 has only 28 days, so the balance 3 days (31 - 28 = 3) overflow to become March 03.

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