Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between '01' and '1' in a JavaScript date [duplicate]

Tags:

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'

like image 353
Nacho Avatar asked Oct 05 '15 18:10

Nacho


People also ask

Is 01 and 1 the same?

They are absolutely identical.

How do I +1 a Date in JavaScript?

const date = new Date('2022-02-21'); const dateCopy = new Date(date. getTime()); dateCopy. setDate(dateCopy. getDate() + 1); // 👇️ Tue Feb 22 2022 console.

What is the difference between new Date () and Date ()?

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.

What does new Date () return?

"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.


1 Answers

What I see after executing locally is

  • Date in local timezone
  • Date in UTC

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.

like image 175
rajuGT Avatar answered Sep 30 '22 17:09

rajuGT