Javascript Date Gotcha

Steven Chetwynd

javascript

Development

122 Words … ⏲ Reading Time: 33 Seconds

2022-12-24 21:11 +0000


I recently discovered that my weather site was displaying the monthly weather charts wrong, but only in Safari. The X axis was showing the dates out of order, “2022-12-1”, “2022-12-10”, “2022-12-11”….

Important to note here is the day in the first date is only a single digit, Firefox, and Chrome can parse it, Safari cannot. The format isn’t right, so correcting that fixed the problem.

The incorrect format was coming from the Postgres database, to convert and group the dates I was using DATE_PART('day', time) which omits the leading 0. This was being concatenated with the year and month, not a great way to format dates, changing it to DATE_TRUNC('day' time) and stripping off the time in the UI fixed this problem.