moment.utc(date)和moment(date).utc()之间的区别 [英] Difference between moment.utc(date) and moment(date).utc()
问题描述
试图了解行为和两者之间的区别:
Trying to understand the behaviour and difference between:
moment.utc(date) and moment(date).utc()
使用"2018-05-31"作为参数:
Using '2018-05-31' as a param:
moment.utc('2018-05-31').format()
将给出:
2018-05-31T00:00:00Z
2018-05-31T00:00:00Z
,而moment('2018-05-31').utc().format()
将给出:
2018-05-31T04:00:00Z
2018-05-31T04:00:00Z
我都在EST时区执行这两个指令.
I am executing both in EST timezone.
推荐答案
第一个 moment.utc(String)
将您的字符串解析为UTC,而后者将您的矩实例转换为UTC模式.
The first moment.utc(String)
parses your string as UTC, while the latter converts your moment instance to UTC mode.
默认情况下,时刻会解析并以当地时间显示.
By default, moment parses and displays in local time.
如果要解析或显示UTC时间,可以使用moment.utc()
而不是moment()
.
If you want to parse or display a moment in UTC, you can use moment.utc()
instead of moment()
.
这使我们获得了Moment.js的有趣功能. UTC模式.
This brings us to an interesting feature of Moment.js. UTC mode.
请参阅本地与UTC与偏移量指南,以了解更多信息UTC模式和本地模式.
See Local vs UTC vs Offset guide to learn more about UTC mode and local mode.
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>
这篇关于moment.utc(date)和moment(date).utc()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!