observablehq / inspector

The Observable standard inspector.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Date formatting is inconsistent with ECMAScript specification.

mbostock opened this issue · comments

So… it turns out that the ECMAScript specification for date strings has different behavior for date-only strings and date-time strings: if a timezone is not specified, the former defaults to UTC, whereas the latter defaults to local time.

new Date("2007-04-22") // 2007-04-22T00:00:00.000Z
new Date("2007-04-22T00:00") // 2007-04-22T07:00:00.000Z

In our current date formatter, we always use local time. But if we’re going to return a date-only string, we should only do that on UTC midnight, and we should use date.getUTCFullYear() etc for the fields, too.

A nice side-effect of this change is that a date input element also returns dates at UTC midnight, and these will now be displayed as bare dates. However, the downside of this change is that people will have to internalize this somewhat surprising inconsistency in the specification.