在Moment.js中从UTC日期字符串创建本地日期 [英] Create a local date from a UTC date string in Moment.js

查看:586
本文介绍了在Moment.js中从UTC日期字符串创建本地日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定UTC日期字符串(格式为:YYYYMMDDHHmmss)我想使用Moment.js创建具有本地时区的日期。我尝试了以下内容:

Given a UTC date string (formatted: YYYYMMDDHHmmss) I'd like to create a date with the local timezone using Moment.js. I have tried the following:

var utcDateStr = '20140101120000';
var localDate = moment.utc(utcDateStr, 'YYYYMMDDHHmmss');

//actual: true
//desired: false
console.log(localDate._isUTC)

//actual: "Wed Jan 01 2014 12:00:00 GMT+0000"
//desired: "Wed Jan 01 2014 07:00:00 GMT-0500"
console.log(localDate.toString())

如果我调用,默认情况下,如何创建在本地时区格式化的日期toString()

推荐答案

只需使用 .local() 功能,添加在1.5.0版本中。 >

Just use the .local() function, added in version 1.5.0.

var localDate = moment.utc(utcDateStr, 'YYYYMMDDHHmmss').local();

这篇关于在Moment.js中从UTC日期字符串创建本地日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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