如何在moment.js中从UTC转换为当地时间? [英] How to convert from UTC to local time in moment.js?

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

问题描述

我有一个简单的用例,但是我不知道如何在moment.js中将GMT/UTC转换为本地时间.

I have a simple use case but I cannot figure out how to convert from GMT/UTC to local time in moment.js.

示例:

var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH").format('YYYY-MMM-DD h:mm A');

console.log(gmtDateTime)发出2015-Oct-24 8:00 PM,这是正确的.现在,我只想将其转换为我的本地时间,恰好是山区夏令时".因此正确的转换日期为2015-Oct-24 2:00 PM,因为我比GMT/UTC早6个小时.

console.log(gmtDateTime) emits 2015-Oct-24 8:00 PM, which is correct. Now I simply want to convert this to my local time, which happens to be Mountain Daylight Time. So the correct converted date would be 2015-Oct-24 2:00 PM, because I am 6 hours earlier than GMT/UTC.

如何使用moment.js轻松完成此操作? (谢谢!)

How can this be done simply with moment.js? (THANKS!)

推荐答案

尝试 moment().local() .

示例:

var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH")
var local = gmtDateTime.local().format('YYYY-MMM-DD h:mm A');

这篇关于如何在moment.js中从UTC转换为当地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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