wrong day is returned in javascript [duplicate] - javascript

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?

No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.

In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.

Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...

In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

Related

Why am I getting incorrect number of days? [duplicate]

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

Date( 'YYYY-MM-DD') constructs a Date that is one day behind than specified (Javascript) [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
Observe the following code and it's output
let str = '2019-06-21';
let dateObj = new Date(str);
console.log(dateObj)
> Thu Jun 20 2019 19:00:00 GMT-0500
The Date object is a day behind than what I specified.
What is a robust way to amend this?
Creating a function to modify the 'DD' portion seems hacky.
I've settled on decrementing the Date object after constructing, but is there a better way?
Why does this behavior happen?
Specify timezone. Currently its defaulting to GMT.
str = '2019-06-21T00:00:00-07:00';
dateObj = new Date(str);
>> Fri Jun 21 2019 00:00:00 GMT-0700 (Pacific Daylight Time)
The date object created is in UTC so when you console it you get the day, month and year in your Timezone
let str = '2019-06-21';
let dateObj = new Date(str);
console.log(dateObj.toDateString())
// expected "Thu Jun 20 2019" based on your timezone.
console.log(dateObj.toUTCString())
// expected "Fri, 21 Jun 2019 00:00:00 GMT"
To create a DateTime based on your timezone, you can either give your timezone or do the following
let d = new Date(Date.now())
d.setDate(21)
d.setHours(0) // if you care to start the day at 00 hrs
d.setMinutes(0) // if you care to start the day at 00 mins
d.setSeconds(0) // if you care to start the day at 00 seconds
console.log(d.toDateString())

JS date compare issue [duplicate]

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

Javascript date - month always increment

Hello I encountered strange behavior with javascript date. I show in this example:
var date = new Date(2017, 07, 22);
console.log(date); //22. 8. 2017
console.log(date.toLocaleDateString()) //Tue Aug 22 2017 00:00:00 GMT+0200
Why is month always increment? Is normal behavior or its my problem? Thanks
Javascript Date's month starts from 0. So 7 is actually 8th Month which is August.
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the JavaScript Date() object, the the month is an integer, starting at 0.
0 = January
1 = February
2 = March
and so on.
Javascript date month starts form zero index only ie jan is 0 and december is 11

toJSON function from Date object substracting one day

I'm doing some date manipulation in javascript using Date object.
I lost like one hour to understund a bug : Right after initialization, I used .toJSON() function and my date was decremented by one. Here's a code sample of what I was doing :
var date = new Date();
console.log(date.getDate()); // print "19"
date.setDate(date.getDate()-1); // print "18"
var formated = date.toJSON().substr(0, 10); // print "2013-09-17"
Suddenly the date moved from 18 to 17.
So to be sure I tried this directly into the developer console :
new Date(2013, 09, 19)
Sat Oct 19 2013 00:00:00 GMT+0200 (Paris, Madrid (heure d’été)) // date "19" as it should
new Date(2013, 09, 19).toJSON()
"2013-10-18T22:00:00.000Z" // date "18" as it shouldn't
Now my question is simply "why ?".
Is it possible that this come from my configuration or else ?
Is it a bug ? If yes is it a known bug ?
If you look more carefully :
new Date(2013, 09, 19);
=> Sat Oct 19 2013 00:00:00 GMT+0200 (CEST)
Two things are important here :
The hour : 00:00:00
The Timezone : +0200
When you then call .toJSON, it will convert it in GMT + 0.
So 00:00:00 - 2hr = Today -1, hour being 22:00:00.
Now take a look at :
new Date(2013, 09, 19).toJSON()
=> "2013-10-18T22:00:00.000Z"
One day before, but hour set to 22:00:00
That's the reason : switching from GMT+0200 to GMT+0.
Now for the solution, someone already asked it : Javascript Date.toJSON don't get the timezone offset ;)
Absolutely no bug here, just checkout the timezone.
The JSON string is in GMT (that's what the Z at the end means). Your local time is two hours ahead of that. So midnight on the 19th in your timezone is 22:00 on the 18th in GMT.
It's because of the -2 hours time offset.
Try to do
YourDate.setHours(0, -YourDate.getTimezoneOffset(), 0 0);
It will be a correct date.

Categories