1

Why does JS Date object change toUTCString on October 10th?

new Date('2017-10-9').toUTCString()
"Sun, 08 Oct 2017 23:00:00 GMT"



new Date('2017-10-10').toUTCString()
"Tue, 10 Oct 2017 00:00:00 GMT"

I am writing these in the UK. BST ends on October 29th. What is going on?!

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
dafyddPrys
  • 898
  • 12
  • 23

1 Answers1

3

In the first example the date is parsed as local date, and in the second as UTC date. To parse the first date as UTC too, add a 0 before 9.

console.log(new Date('2017-10-09').toUTCString()); // Mon, 09 Oct 2017 00:00:00 GMT

Inconsistencies in date parsing like that are why you should always pass a date in ISO-8601 format to the Date constructor. You can also use a library like Moment.js.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177