-4

There are two strange things:

  • day 30 instead of 31?
  • it set 13 as time instead of 00?

    new Date('Dec 31, 2019').toISOString()
    // '2019-12-30T13:00:00.000Z'
    

Basically what I'm trying to do is to convert original data format into YYYY-MM-DD.

Alex Craft
  • 13,598
  • 11
  • 69
  • 133
  • 3
    It seems you are in a +11 time zone and this shows you the UTC time, so it's correct, it just doesn't have the time offset. – VLAZ Jul 28 '19 at 19:55
  • 1
    You forgot to take the timezone into account. – axiac Jul 28 '19 at 20:01

1 Answers1

1

new Date('Dec 31, 2019') - this date will be shown in your local time format.

e.g.:

`Tue Dec 31 2019 00:00:00 GMT+1100` (Your Standard Time)

However, new Date('Dec 31, 2019').toISOString() - this date will be shown like

2019-12-30T13:00:00.000Z. It means Dec 31, 2019 minus 11 hours(your time zone).

In addition, to avoid strange behaviors, try to set explicitly year, month and day to avoid strange behavior.

You should parse Date in the following format:

// yyyy-mm-dd format
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
let yourDate = new Date(parts[0], parts[1]-1, parts[2]); // Be careful: months are 0-based

You can see valid Date times here.

StepUp
  • 36,391
  • 15
  • 88
  • 148