YMD Revisited

In Year, Month, Day, I explained an annoying problem with Javascript dates, and outlined the solution I took to avoid it in Moyase (which only needs to deal with dates in local time, and shouldn’t care if you move your device across timezones). Well, as it turns out, even this solution was not foolproof, and I totally should have seen it coming.

See, Date.toISOString() does indeed return a pretty nicely-formatted string that I can carve up and use for date components…the (obvious, in retrospect) problem is that it gives it to you in UTC time with a timezone offset, which can very well be a totally different calendar day than in local time. I probably would have never noticed this until I found myself on the other side of the world at some point in the future, or happened to be working in the app very early in the morning, so I’m very lucky to have a friend in Australia as an early adopter; she noticed that at certain times in the day, her work would be counted towards the previous day and her calendar wouldn’t display the right dates.

The fix ended up being pretty simple; instead of this:

return date.toISOString().substr(10);

I went with this instead:

return [
    String(date.getFullYear()).padStart(4, '0'),
    String(date.getMonth() + 1).padStart(2, '0'),
    String(date.getDate()).padStart(2, '0'),

With this, the app works correctly no matter the timezone or time of day. Hopefully this is the last time I have to think about this problem.