简体   繁体   English

Javascript:如何将UTC日期转换为本地日期?

[英]Javascript: how to convert a UTC date to local one?

Looks like a very simple thing to do? 看起来很简单吗? Not afrer reading this http://dygraphs.com/date-formats.html - what a mess! 再次阅读此http://dygraphs.com/date-formats.html-真是一团糟!

var time_utc="2016-04-25 20:19:00.306671";
document.write("Local date:"+new Date(time_utc+" UTC")); // Firefox 44.0.2: Invalid Date 

How do I print a date in above format adjusted to local time? 如何以上述格式打印调整为当地时间的日期?

Have you looked into Moment.js? 您是否研究过Moment.js? http://momentjs.com/ It's a handy date-object wrapper that makes date object manipulation easy. http://momentjs.com/这是一个方便的日期对象包装器,可简化日期对象的操作。 Particularly, the local() function provided will give you what you need here. 特别是,提供的local()函数将为您提供所需的信息。

All you have to do is install moment from npm and then include it in your js file at the top like this: 您要做的就是从npm安装时刻,然后将其包含在顶部的js文件中,如下所示:

var moment = require("moment");

Then to change your time_utc variable to local all you have to do is: 然后将time_utc变量更改为local,您要做的就是:

var time_utc="2016-04-25 20:19:00.307";
document.write("Local date:"+moment(time_utc).local());

As someone advised me before, it is not wise to include an entire library for a simple, one time function. 正如以前有人建议我的那样,为一个简单的一次性函数包含整个库是不明智的。 As a disclaimer, my work requires me to do many date-time calculations and conversions throughout a large project, so including a library for ease is much preferred in my case. 作为免责声明,我的工作要求我在一个大型项目中进行许多日期时间的计算和转换,因此在我的情况下,最好包括一个易于使用的库。 However, if you only have to use it this one time, my answer may not be the best. 但是,如果您只需要使用一次,我的答案可能不是最好的。

You need append UTC to the string before converting it to a local date in js: 您需要在字符串中附加UTC,然后再将其转换为js中的本地日期:

var date = new Date('25/04/2016 4:52:48 PM UTC');
date.toString() // "Mon Apr 25 2016 09:52:48 GMT-0700 (PDT)"

The article you provided mentions halfway through the page, 您提供的文章在页面中途提到,

Using hyphens (-) instead of slashes (/) works in WebKit browsers, but not in IE or FF. 在WebKit浏览器中,使用连字符(-)代替斜杠(/),但在IE或FF中不起作用。 Beware the UTC parse of YYYY-MM-DD! 当心YYYY-MM-DD的UTC解析!

As well as, 以及,

Don't try to specify milliseconds. 不要尝试指定毫秒。 Only recent versions of Chrome will understand you. 只有最新版本的Chrome才能了解您。

With the date 2016-04-25 20:19:00.306671 you use hyphens and milliseconds. 与日期2016-04-25 20:19:00.306671您使用连字符和毫秒。 You could modify your time_utc string a bit to make it compatible like so, 您可以像这样修改一下time_utc字符串以使其兼容,

var time_utc = "2016-04-25 20:19:00.306671";
time_utc = time_utc.replace(/-/g, "/"); 
time_utc = time_utc.split(".").shift();

var d = new Date(time_utc);
d.toString();

The above code outputs, 上面的代码输出,

Mon Apr 25 2016 20:19:00 GMT+0200 (CEST)

If you use moment.js , you can use: 如果您使用moment.js ,则可以使用:

var time_utc = "2016-04-25 20:19:00.306671";

var localDate = moment.utc(time_utc).local();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM