DateTime从UTC以本地时间格式 [英] DateTime from UTC in Local Time format
问题描述
我有一个日期时间
,它以 UTC
格式保存在服务器上。
日期返回给客户端如下所示: 2015-02-05T07:52:27.59
我需要一个解析日期字符串的JavaScript函数,并将其转换为客户端的本地时间
。
例如,javascript应该在指定的时间内添加2个小时,因为客户端在UTC +2上。
我已经尝试过很多例子互联网,但不是他们正在工作。
我希望是一件容易的事,考虑到这是在互联网上发生。
拆分
字符串,零参考月份,使用 Date.UTC
创建新的 Date
对象。输出 toString
是本地时间,除非你指定其他东西。
var dateTime = 2015-02-05T07:52:27.59',parts = dateTime.split(/ [ - T:\。] / g); parts [1] - = 1; document.body.textContent = new Date(Date.UTC .apply(null,parts))。toString();
<或者,如果您有一个现代浏览器。将
Z
追加到字符串,并依赖特定浏览器的 Date
解析。
var dateTime ='2015-02-05T07:52:27.59Z'; document.body.textContent = new Date(dateTime).toString();
I have a Date Time
which is saved on the server in UTC
format.
The date is returned to the client looks like this: 2015-02-05T07:52:27.59
I need a javascript function which parses the date string, and converts it to the local time
of the client.
Example, the javascript should add 2 hours to specified time, because the client is on on UTC +2.
I have tried many examples on the internet but non of them was working.
I expect to be something easy, considering that this is happening all over the internet.
Split
the string, zero reference the month, use Date.UTC
when creating the new Date
object. The output toString
is local time unless you specify something else.
var dateTime = '2015-02-05T07:52:27.59',
parts = dateTime.split(/[-T:\.]/g);
parts[1] -= 1;
document.body.textContent = new Date(Date.UTC.apply(null, parts)).toString();
Alternatively, if you have a modern browser. Append Z
to the string and rely on the Date
parse of the particular browser.
var dateTime = '2015-02-05T07:52:27.59Z';
document.body.textContent = new Date(dateTime).toString();
这篇关于DateTime从UTC以本地时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!