gpt4 book ai didi

javascript - momentjs - 如何尊重服务器日期时间

转载 作者:行者123 更新时间:2023-11-28 01:13:55 24 4
gpt4 key购买 nike

我正在解析通过ajax从服务器“2014-06-06T10:21:11+00:00”接收到的日期时间

var mom = moment(datetime);             
return mom.format("H:mm");

结果我得到:

18:21

换算成我的时区是 10:21。如何让 momentjs 尊重服务器时区?

最佳答案

该格式不是 Moment.js 默认遵循的格式(请参阅 list of supported formats )。但是,您可以使用 .utc()获得正确结果的方法,如下所示:

var mom = moment.utc(datetime);
return mom.format("H:mm");

文档中的相关片段:

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

This brings us to an interesting feature of Moment.js. UTC mode.

While in UTC mode, all display methods will display in UTC time instead of local time.

关于javascript - momentjs - 如何尊重服务器日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com