What is the difference between the dates '2015-10-01' and '2015-10-1' in JavaScript?
new Date('2015-10-1')
This returns 'Thu Oct 01 2015 00:00:00 GMT-0300'
new Date('2015-10-01')
Returns 'Wed Sep 30 2015 21:00:00 GMT-0300'
They are absolutely identical.
const date = new Date('2022-02-21'); const dateCopy = new Date(date. getTime()); dateCopy. setDate(dateCopy. getDate() + 1); // 👇️ Tue Feb 22 2022 console.
Date() returns an implementation dependent string representing the current date and time. new Date() returns a Date object that represents the current date and time. ;-) Yeah, I know.
"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.
What I see after executing locally is
As per the MDN docs, Date.parse
will assume the date to be UTC format if it has complete DD
else it will assume in local timezone format.
Detailed explanation regarding month change: (comments)
new Date('2015-10-1')
when you execute this statement output is 'Thu Oct 01 2015 00:00:00 GMT-0300'. i.e. its your local time and it is GMT -3hrs.
But when you execute the new Date('2015-10-01')
the output is 'Wed Sep 30 2015 21:00:00 GMT-0300' which is in UTC time. i.e. 3hrs minus from your local time.
So it is 1st Oct midnight -3hrs, which is previous day's 21hrs. i.e. Sep 30 21hrs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With