我想知道哪个库做得正确。我认为它是卢克森,因为它输出了我想要的东西,但现在我不确定哪一个是正确的。3月17日将是夏令时。我以UTC建立时间,将时区更改为PST,然后再转换回来。卢克逊的UTC时间为上午8点,夏令时为上午9点,但Dayjs的UTC时间则为上午7点,夏令营为上午8点整。
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());
上述代码的输出为
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
您确定在Day.js中正确配置了时区支持吗?它们产生相同的结果:
<script src="https://unpkg.com/dayjs@1.11.0/plugin/utc.js"></script>
<script src="https://unpkg.com/dayjs@1.11.0/plugin/timezone.js"></script>
<script src="https://unpkg.com/dayjs@1.11.0/dayjs.min.js"></script>
<script type="module">
import {DateTime} from 'https://unpkg.com/luxon@2.3.1/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>