🏠 back to Observable

Odd date behaviour when setting hour to 17

For some reason in Observable (on a Mac, Chrome)

new Date(2019, 7, 8, 17, 0, 0, 0)

prints as

2019-08-09

which is obviously incorrect, while in the console it prints corrects as

Thu Aug 08 2019 17:00:00 GMT-0700 (Pacific Daylight Time)

I only find this behaviour when the hour is 17 (5pm).
37%20PM

Odd, eh?

Sure, what’s going on is in the notebook-inspector project, which is open source, and here’s the source: https://github.com/observablehq/inspector/blob/master/src/formatDate.js

5pm in Pacific Daylight Time is midnight in UTC time, so we format it as a short date:

+new Date(2019, 7, 8, 17, 0, 0, 0) ===
  +new Date(Date.UTC(2019, 7, 9, 0, 0, 0, 0))

That said, I think we are a little inconsistent in that the midnight “date” handling is UTC-centric but the default date formatting shows times in the local timezone. Perhaps we should either show formatted datetimes in UTC or indicate their timezone.

3 Likes

Ah, I see. Makes sense. Yes, either solution would be an improvement (either show the local timezone or show as UTC)

Thanks!