dockerman_20
dockerman_20

Reputation: 55

Luxon vs DayJS converting to UTC when in DST

I'm wondering which library is doing this correctly. I think it is Luxon because it outputs what I want, but now I am not sure which is correct. Basically March 17 will be in DST. I establish a time in UTC, I change the timezone to PST, then I convert back. Luxon produces 8am UTC which is 9am with DST, but Dayjs produces 7am UTC which is 8am with DST.

  const dj1 = dayjs("2022-03-17T16:00:00Z");
  const djtz1 = dj1.tz("America/Los_Angeles");
  const lux1 = DateTime.fromJSDate(d).setZone("America/Los_Angeles");
  console.log(djtz1.utc().format());
  console.log(lux1.setZone("utc").toISO());

The output of the above code is

DayJS
2022-03-17T15:00:00Z 
Luxon
2022-03-17T16:00:00.000Z 

https://codesandbox.io/s/luxon-playground-forked-k6rfcl?file=/src/index.js

Upvotes: 2

Views: 2995

Answers (2)

Hasan Mothaffar
Hasan Mothaffar

Reputation: 61

Dayjs doesn't support DST. There's a GitHub issue regarding this, which has been opened since 2020: https://github.com/iamkun/dayjs/issues/1271.

I migrated my app to use Luxon because DST is important for us. Check out the timezones docs for Luxon here: https://github.com/moment/luxon/blob/master/docs/zones.md

Side note: there's a noticeable difference in bundle size between the two libraries. I'm using Luxon on server-side so bundle size is not as important as it is on client-side. You might as well check other options for date libraries (date-fns, moment.js) but first check if they support DST.

Upvotes: 1

jsejcksn
jsejcksn

Reputation: 33796

Are you sure that you correctly configured timezone support in Day.js? They produce the same results:

<script src="https://unpkg.com/[email protected]/plugin/utc.js"></script>
<script src="https://unpkg.com/[email protected]/plugin/timezone.js"></script>
<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
<script type="module">

import {DateTime} from 'https://unpkg.com/[email protected]/src/luxon.js';

const {dayjs} = window;
dayjs.extend(window.dayjs_plugin_utc);
dayjs.extend(window.dayjs_plugin_timezone);

const isoString = '2022-03-17T16:00:00Z';

const dj1 = dayjs(isoString);
const djtz1 = dj1.tz('America/Los_Angeles');

const lux1 = DateTime.fromISO(isoString).setZone('America/Los_Angeles');

console.log({dayjs: djtz1.utc().format()});
console.log({luxon: lux1.setZone('utc').toISO()});

</script>

Upvotes: -1

Related Questions