Moment js 处理祖鲁日期时间
Moment js handle zulu date time
我在从 api 检索数据并在当地时间显示时遇到问题。
API 向我打印 zulu datetime 2021-11-12T14:30:29.000000Z
我已经尝试将其转换为 Europe/Rome gtm 但任何人都工作过,考虑到 olso DST..
moment(d.acktime, "YYYYMMDDHHmmss").utcOffset('Europe/Rome').format("HH:mm");
moment(d.acktime, "YYYYMMDDHHmmss").tz("Europe/Rome").format("HH:mm");
显示的数据始终是来自 api 的祖鲁图姆。有什么想法吗??
Moment 将 parse ISO 8601 而不指定格式:
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string)
if a known format is not found.
只需确保使用时刻时区(带数据)库即可。
const d = { acktime: '2021-11-12T14:30:29.000000Z' };
const formatted = moment(d.acktime).tz('Europe/Rome').format('HH:mm');
console.log(formatted); // 15:30 (Rome is GMT+1)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone-with-data.min.js"></script>
时间是 EOL, so I recommend Luxon as an alternative. It is created by the same developers. Setting the timezone is also easier and does not require additional plugins and libraries. See "Time zones and offsets" 了解更多信息。
const { DateTime } = luxon;
const d = { acktime: '2021-11-12T14:30:29.000000Z' };
const formatted = DateTime.fromISO(d.acktime).setZone('Europe/Rome').toFormat('HH:mm');
console.log(formatted); // 15:30 (Rome is GMT+1)
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.1.1/luxon.min.js"></script>
我在从 api 检索数据并在当地时间显示时遇到问题。
API 向我打印 zulu datetime 2021-11-12T14:30:29.000000Z
我已经尝试将其转换为 Europe/Rome gtm 但任何人都工作过,考虑到 olso DST..
moment(d.acktime, "YYYYMMDDHHmmss").utcOffset('Europe/Rome').format("HH:mm");
moment(d.acktime, "YYYYMMDDHHmmss").tz("Europe/Rome").format("HH:mm");
显示的数据始终是来自 api 的祖鲁图姆。有什么想法吗??
Moment 将 parse ISO 8601 而不指定格式:
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of
new Date(string)
if a known format is not found.
只需确保使用时刻时区(带数据)库即可。
const d = { acktime: '2021-11-12T14:30:29.000000Z' };
const formatted = moment(d.acktime).tz('Europe/Rome').format('HH:mm');
console.log(formatted); // 15:30 (Rome is GMT+1)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone-with-data.min.js"></script>
时间是 EOL, so I recommend Luxon as an alternative. It is created by the same developers. Setting the timezone is also easier and does not require additional plugins and libraries. See "Time zones and offsets" 了解更多信息。
const { DateTime } = luxon;
const d = { acktime: '2021-11-12T14:30:29.000000Z' };
const formatted = DateTime.fromISO(d.acktime).setZone('Europe/Rome').toFormat('HH:mm');
console.log(formatted); // 15:30 (Rome is GMT+1)
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.1.1/luxon.min.js"></script>