javascript - moment.utc(date) 和 moment(date).utc() 之间的区别
问题描述
试图了解以下行为和区别:
moment.utc(date) and moment(date).utc()
使用 '2018-05-31' 作为参数:
moment.utc('2018-05-31').format()
会给:
2018-05-31T00:00:00Z
同时moment('2018-05-31').utc().format()
会给出:
2018-05-31T04:00:00Z
我在 EST 时区执行两者。
解决方案
第一个moment.utc(String)
将您的字符串解析为 UTC,而后者将您的时刻实例转换为 UTC 模式。
默认情况下,moment 解析并以本地时间显示。
如果要解析或显示 UTC 时刻,可以使用
moment.utc()
代替moment()
.这给我们带来了 Moment.js 的一个有趣的特性。UTC 模式。
请参阅Local vs UTC vs Offset指南以了解有关 UTC 模式和本地模式的更多信息。
console.log( moment.utc('2018-05-31').format() );
console.log( moment('2018-05-31').utc().format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
推荐阅读
- arduino - 当我使用字母“&”时,ESP32 writeStringEEPROM 添加了不需要的成员
- react-native - ReactNative AsyncStorage 返回奇怪的值
- javascript - 缓冲区中的 Blob 返回损坏的 pdf
- python - 从字典中迭代值以在 Pandas 中的列之间进行操作
- winforms - MyForm2:未声明的标识符
- python - 在 Python 中计算加速度
- c# - 选择每个导航属性的第一个
- google-forms - 根据电子表格和其他问题更改谷歌表单问题
- r - 使用 R 在宽数据中的列名中插入点
- python-3.x - 了解带有两个变量的 python 打印输出