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.
Related
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))}
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;
}
Javascript running in Safari has a problem with my birthday. This sometimes breaks validation in forms for me (as forms check the input with the javascript output, and the days don't match anymore). I'd love to know why this happens.
My birthday is 6th Oct 1985. In Javascript, I create this using:
new Date(1985, 09, 06)
(Note that months are zero-indexed, but days and years are not.)
// My birthday
console.log("1985, 09, 06: " + new Date(1985, 09, 06))
// One day earlier/later
console.log("1985, 09, 07: " + new Date(1985, 09, 07))
console.log("1985, 09, 05: " + new Date(1985, 09, 05))
// One month earlier/later
console.log("1985, 10, 06: " + new Date(1985, 10, 06))
console.log("1985, 08, 06: " + new Date(1985, 08, 06))
// One year earlier/later
console.log("1986, 09, 06: " + new Date(1986, 09, 06))
console.log("1984, 09, 06: " + new Date(1984, 09, 06))
Results running this in Safari are:
// My birthday (note the day is wrong, and the time is 11pm)
Sat Oct 05 1985 23:00:00 GMT+1000 (AEST)
// One day earlier/later
Mon Oct 07 1985 00:00:00 GMT+1100 (AEDT)
Sat Oct 05 1985 00:00:00 GMT+1000 (AEST)
// One month earlier/later
Wed Nov 06 1985 00:00:00 GMT+1100 (AEDT)
Fri Sep 06 1985 00:00:00 GMT+1000 (AEST)
// One year earlier/later
Mon Oct 06 1986 00:00:00 GMT+1100 (AEDT)
Sat Oct 06 1984 00:00:00 GMT+1000 (AEST)
https://jsfiddle.net/27bupLr9/
In Chrome, my birthday is created correctly:
console.log(new Date(1985, 09, 06))
Sun Oct 06 1985 00:00:00 GMT+1000 (AEST)
This has been resolved in macOS High Sierra.
https://jsfiddle.net/8cvhnwgs/1/
//new Date(year, month, day, hours, minutes, seconds, milliseconds)
$("#out").html(new Date(2015, 5, 31, 08, 25, 30, 0));
The date I give is May 31st, but it outputs July First, can someone explain why? Have I used arguments wrong?
The month parameter to the Date constructor is 0 indexed, so 5 is June, which only has 30 days.
Look at the Date. The month is an integer value starting from 0 to 11.
new Date(2015, 0, 31)
// Sat Jan 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 1, 31)
// Tue Mar 03 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 2, 31)
// Tue Mar 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 3, 31)
// Fri May 01 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 4, 31)
// Sun May 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 5, 31)
// Wed Jul 01 2015 00:00:00 GMT+0000 (UTC)
Try this:
$("#out").html(new Date(2015, 4, 31, 08, 25, 30, 0));
5 is June, and since there is no 31 in June, it goes to July 1st.
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.