Nacho Nacho - 2 months ago 7x
Javascript Question

Difference between '01' and '1' in a JavaScript date

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')

'Wed Sep 30 2015 21:00:00 GMT-0300'


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.