javascript 将UTC日期时间转换为本地日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15316656/
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 datetime to local datetime
提问by RockOnGom
I tried many times to convert utc datetime to local datetime,and I have failed. My utc datetime format is
我多次尝试将 utc 日期时间转换为本地日期时间,但都失败了。我的UTC日期时间格式是
Fri Mar 8 23:12:27 UTC+0200 2013
2013 年 3 月 8 日星期五 23:12:27 UTC+0200
Also my JavaScript code is
我的 JavaScript 代码也是
var time = Date(param_time);//param_time is /Date(1362866006646)/
And then time is being Sun Mar 10 00:21:54 UTC+0200 2013I need to datetime as 2008-01-28T20:24:17Zbecause I will convert local datetime to pretty datetime.
然后时间是Sun Mar 10 00:21:54 UTC+0200 2013我需要将日期时间设为2008-01-28T20:24:17Z因为我会将本地日期时间转换为漂亮的日期时间。
http://ejohn.org/files/pretty.js
http://ejohn.org/files/pretty.js
How can I do this ? I looked at many questions on stackoverflow but no one does it work. Thank you.
我怎样才能做到这一点 ?我看了很多关于 stackoverflow 的问题,但没有一个能奏效。谢谢你。
回答by Zeta
In order to format your Date
correctly use toISOString()
:
为了Date
正确格式化您,请使用toISOString()
:
var time = param_time.toISOString();
Note that param_time
needs to be a valid Date
object.
请注意,param_time
需要是一个有效的Date
对象。
References
参考
- MDN:
Date
(sections: Syntax, Example: ISO 8601 formatted dates)
回答by Panayot Karabakalov
I rarely use javascript and all this date time conversion are mystery to me as well, javascript is a client side technology and all this "UTC" phrases means nothing (at least to me), as all the kind of getUTC...()/setUTC...()
functions works in local time, the same is goes for all Date.to...String()
functions, even the new Date()
(that due to the docs) s'd be initialized in UTC, also give a local time.
我很少使用 javascript,所有这些日期时间转换对我来说也是个谜,javascript 是一种客户端技术,所有这些“UTC”短语都没有任何意义(至少对我来说),因为所有类型的getUTC...()/setUTC...()
功能都在本地时间工作,所有Date.to...String()
函数也是如此,即使new Date()
(由于文档)s'd 在UTC中初始化,也给出本地时间。
However, if you have a (correct) date in UTC and wish to convert it to current (client side) local time, then you need getTimezoneOffset()
, or shortly:
但是,如果您有一个(正确的)UTC 日期并希望将其转换为当前(客户端)本地时间,那么您需要getTimezoneOffset()
,或者很快:
function UTCToLocalTime(d) {
var timeOffset = -((new Date()).getTimezoneOffset()/60);
d.setHours(d.getHours() + timeOffset);
return d;
}
var time = new Date(Date.parse('Fri Mar 8 23:12:27 UTC+0200 2013'));
alert(UTCToLocalTime(time)); // Sat Mar 9 01:12:27 UTC+0200 2013
//p.s. or...
function UTCToLocalTime2(d)
{
return new Date(d.toString().replace(/UTC.*/g,"") + d.getYear());
}
回答by EliaszKubala
var timezone = "UTC+01:30";
var start = new Date();
if(timezone != "UTC"){
timezone = timezone.replace(/UTC/g,"");
znak = timezone.charAt(0);
timezone = timezone.replace(/[+|-]/g,"");
timezone = timezone.split(":");
//var start = new Date(start.toString() + " " + timezone);e.
//alert(start.toString());
if(znak == "+") start = new Date(start.getTime() + (timezone[0]*60*60000 + timezone[1] * 60000) );
if(znak == "-") start = new Date(start.getTime() - (timezone[0]*60*60000 + timezone[1] * 60000) );
}
var hours = start.getUTCHours();
var minutes = start.getUTCMinutes();
var seconds = start.getUTCSeconds();
回答by Erick Wendel
var day = 10;
var month = 04;
var year = 2015;
var dateUtc = Date.UTC(year, month - 1, day + 1, 0, 0, 0);
> 1428710400000
var toDate = new Date(dateUtc);
> Fri Apr 10 2015 21:00:00 GMT-0300 (Hora oficial do Brasil)