William M.
William M.

Reputation: 379

new Date() returns wrong date

I know this is a very basic question, but I am stuck on this for several hours. Why do I get the wrong date and time, when I pass my ISO String to new Date()

new Date('2017-08-01T00:00:00');
=> 2017-07-31T22:00:00.000Z

Upvotes: 0

Views: 5756

Answers (2)

Rajib Dey
Rajib Dey

Reputation: 54

new Date('2017-08-01T00:00:00').toISOString() => 2017-07-31T18:30:00.000Z  (my timezone is +530)
new Date('2017-08-01T00:00:00.000Z').toISOString() => 2017-08-01T00:00:00.000Z (input is in UTC)
new Date('2017-08-01T00:00:00.000+0530').toISOString() => 2017-07-31T18:30:00.000Z
new Date('2017-08-01T00:00:00.000+0200').toISOString() => 2017-07-31T22:00:00.000Z

in your case, input date is not UTC and your system timezone is +0200 so you see the time difference. Second example shows there is no change in case of UTC.

Hope above examples clarify it.

Upvotes: 2

Xeewi
Xeewi

Reputation: 39

The returned date is an ISO 8601 date. This is the good date :)

const test = new Date('2017-08-01T00:00:00');
const isoString = test.toISOString()
const dateString = test.toDateString()

console.log('iso', isoString)
console.log('date', dateString)

Upvotes: -1

Related Questions