简体   繁体   English

将UTC日期时间转换为本地日期时间

[英]Convert UTC datetime to local datetime

I tried many times to convert utc datetime to local datetime,and I have failed. 我多次尝试将utc datetime转换为本地日期时间,但我失败了。 My utc datetime format is 我的utc日期时间格式是

Fri Mar 8 23:12:27 UTC+0200 2013 3月8日星期五23:12:27 UTC + 0200 2013

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 2013 I need to datetime as 2008-01-28T20:24:17Z because I will convert local datetime to pretty datetime. 然后时间是太阳3月10日00:21:54 UTC + 0200 2013我需要将datetime作为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. 我在stackoverflow上查看了很多问题,但是没有人能够正常工作。 Thank you. 谢谢。

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 参考

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...()至少对我而言),因为所有类型的getUTC...()/setUTC...()函数在本地时间工作,所有Date.to...String()函数都是一样的,甚至new Date() (由于文档)也会在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());
}
  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();
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)

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

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