Javascript 将UTC日期时间转换为本地日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6525538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert UTC date time to local date time
提问by Amr Elgarhy
From the server I get a datetime variable in this format: 6/29/2011 4:52:48 PM
and it is in UTC time. I want to convert it to the current user's browser time using JavaScript.
我从服务器得到一个这种格式的日期时间变量:6/29/2011 4:52:48 PM
它是 UTC 时间。我想使用 JavaScript 将其转换为当前用户的浏览器时间。
How this can be done using JavaScript or jQuery?
如何使用 JavaScript 或 jQuery 做到这一点?
回答by digitalbath
Append 'UTC' to the string before converting it to a date in javascript:
在将字符串转换为 javascript 中的日期之前,将“UTC”附加到字符串中:
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
回答by Hulvej
In my point of view servers should always in the general case return a datetime in the standardized ISO 8601-format.
在我看来,服务器在一般情况下应该始终以标准化的 ISO 8601 格式返回日期时间。
More info here:
更多信息在这里:
IN this case the server would return '2011-06-29T16:52:48.000Z'
which would feed directly into the JS Date object.
在这种情况下,服务器将返回'2011-06-29T16:52:48.000Z'
,这将直接提供给 JS 日期对象。
var utcDate = '2011-06-29T16:52:48.000Z'; // ISO-8601 formatted date returned from server
var localDate = new Date(utcDate);
The localDate
will be in the right local time which in my case would be two hours later (DK time).
该localDate
会在正确的本地时间这在我的情况下,将在两个小时后(DK时间)。
You reallydon't have to do all this parsing which just complicates stuff, as long as you are consistent with what format to expect from the server.
您真的不必进行所有这些只会使事情复杂化的解析,只要您与服务器期望的格式一致即可。
回答by Adorjan Princz
This is an universal solution:
这是一个通用的解决方案:
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime()+date.getTimezoneOffset()*60*1000);
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
Usage:
用法:
var date = convertUTCDateToLocalDate(new Date(date_string_you_received));
Display the date based on the client local setting:
根据客户端本地设置显示日期:
date.toLocaleString();
回答by Nobita
You should get the (UTC) offset (in minutes) of the client:
您应该获得客户端的 (UTC) 偏移量(以分钟为单位):
var offset = new Date().getTimezoneOffset();
And then do the correspondent adding or substraction to the time you get from the server.
然后对您从服务器获得的时间进行相应的加法或减法。
Hope this helps.
希望这可以帮助。
回答by PramodB
For me above solutions didn't work.
对我来说,上述解决方案不起作用。
With IE the UTC date-time conversion to local is little tricky.
For me, the date-time from web API is '2018-02-15T05:37:26.007'
and I wanted to convert as per local timezone so I used below code in JavaScript.
使用 IE 将 UTC 日期-时间转换为本地是有点棘手的。对我来说,来自 Web API 的日期时间是'2018-02-15T05:37:26.007'
,我想根据本地时区进行转换,所以我在 JavaScript 中使用了以下代码。
var createdDateTime = new Date('2018-02-15T05:37:26.007' + 'Z');
回答by Molp Burnbright
This works for me:
这对我有用:
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime() - date.getTimezoneOffset()*60*1000);
return newDate;
}
回答by Ben Bryant
Put this function in your head:
把这个功能放在你的脑海里:
<script type="text/javascript">
function localize(t)
{
var d=new Date(t+" UTC");
document.write(d.toString());
}
</script>
Then generate the following for each date in the body of your page:
然后为页面正文中的每个日期生成以下内容:
<script type="text/javascript">localize("6/29/2011 4:52:48 PM");</script>
To remove the GMT and time zone, change the following line:
要删除 GMT 和时区,请更改以下行:
document.write(d.toString().replace(/GMT.*/g,""));
回答by Uniphonic
After trying a few others posted here without good results, this seemed to work for me:
在尝试了其他发布在这里但没有很好结果的其他人之后,这似乎对我有用:
convertUTCDateToLocalDate: function (date) {
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
}
And this works to go the opposite way, from Local Date to UTC:
这适用于相反的方式,从本地日期到 UTC:
convertLocalDatetoUTCDate: function(date){
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
}
回答by pabloa98
Add the time zone at the end, in this case 'UTC':
在末尾添加时区,在本例中为“UTC”:
theDate = new Date( Date.parse('6/29/2011 4:52:48 PM UTC'));
after that, use toLocale()*function families to display the date in the correct locale
之后,使用toLocale()*函数族以正确的语言环境显示日期
theDate.toLocaleString(); // "6/29/2011, 9:52:48 AM"
theDate.toLocaleTimeString(); // "9:52:48 AM"
theDate.toLocaleDateString(); // "6/29/2011"
回答by pabloa98
Use this for UTC and Local time convert and vice versa.
将此用于 UTC 和本地时间转换,反之亦然。
//Covert datetime by GMT offset
//If toUTC is true then return UTC time other wise return local time
function convertLocalDateToUTCDate(date, toUTC) {
date = new Date(date);
//Local time converted to UTC
console.log("Time: " + date);
var localOffset = date.getTimezoneOffset() * 60000;
var localTime = date.getTime();
if (toUTC) {
date = localTime + localOffset;
} else {
date = localTime - localOffset;
}
date = new Date(date);
console.log("Converted time: " + date);
return date;
}