javascript toISOString()忽略时区偏移 [英] javascript toISOString() ignores timezone offset
问题描述
我正在尝试将Twitter datetime转换为本地的iso字符串(对于prettyDate),现在为2天。我只是没有得到当地时间。..
I am trying to convert Twitter datetime to a local iso-string (for prettyDate) now for 2 days. I'm just not getting the local time right..
im使用以下功能:
function getLocalISOTime(twDate) {
var d = new Date(twDate);
var utcd = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(),
d.getMinutes(), d.getSeconds(), d.getMilliseconds());
// obtain local UTC offset and convert to msec
localOffset = d.getTimezoneOffset() * 60000;
var newdate = new Date(utcd + localOffset);
return newdate.toISOString().replace(".000", "");
}
在newdate中,一切都可以,但是toISOString()将它抛回原来时间再次...
任何人都可以帮助我获得当地时间的从twdd格式化为:
Thu,2012年5月31日08:33:41 +0000
in newdate everything is ok but the toISOString() throws it back to the original time again... Can anybody help me get the local time in iso from the Twitterdate formatted as: Thu, 31 May 2012 08:33:41 +0000
推荐答案
moment.js
很棒,但有时你不想拉一个数量的依赖关系简单的东西。
moment.js
is great but sometimes you don't want to pull a lage number of dependencies for a simple things.
以下工作:
var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0,-1);
// => '2015-01-26T06:40:36.181'
,-1)
摆脱尾随的 Z
,表示祖鲁时区,可以替换为您自己的。
The slice(0,-1)
gets rid of the trailing Z
which represents Zulu timezone and can be replaced by your own.
这篇关于javascript toISOString()忽略时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!