Moment.js将本地时间转换为UTC时间有效 [英] Moment.js Convert Local time to UTC time does work

查看:873
本文介绍了Moment.js将本地时间转换为UTC时间有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Moment.js将本地时间转换为UTC等效时间.我相信我有正确的方法,但是不会改变时间.

I would like to use Moment.js to convert a local time to UTC equivalent. I believe that I have the correct method in place, but it does not alter the time.

我在澳大利亚的悉尼+11,预计UTC时间会早11个小时.

I'm in Sydney Australian +11 and expect the UTC time to be 11 hours earlier.

在对象的内部,isUTC标志从false变为true,但是时间没有变化,我是否打算为此使用其他技术.

Internally on the moment object the isUTC flag changes from false to true, but the time does NOT shift, am I meant to use a different technique for this.

实际上如何从该对象中获取当前UTC日期

How do I actually get the current UTC date out of this object

转换前

var val = '18/03/2015';
var selectedDate = moment(val, 'DD/MM/YYYY');

转换后

var a = selectedDate.utc()

推荐答案

我刚刚尝试了这段代码,看来我获得了正确的UTC时间.我想我只是想确认我在做什么是从moment.js访问UTC时间的正确方法

I just tried this code and it seems like I get the correct UTC time. I guess I just want to confirm that what I am doing is correct way to access the UTC time from moment.js

a.format("YYYY-MM-DD HH:mm:ssZ")

我发现我在应用程序中的使用模式不正确

I found that my usage pattern of in my application was incorrect

selectedDate.utc().format(fullFormat)

应该是

moment.utc(selectedDate).format(fullFormat)

这篇关于Moment.js将本地时间转换为UTC时间有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆