本文介绍了如何在javascript中将ISOString转换为本地ISOString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在javascript中将ISOString转换为本地ISOString?
How to convert ISOString to local ISOString in javascript?
我有样式字符串(例如'2013-02-18T16:39:17 + 00:00')
I have ISO 8086 style string(e.g. '2013-02-18T16:39:17+00:00')
我想要将其转换为本地ISO_8601样式字符串...
And I want to convert that to local ISO_8601 style string...
'2013-02-18T16:39:17+00:00' -> '2013-02-19T01:39:17+09:00'
我该怎么办? / p>
What should I do?
推荐答案
只有,但不会使用本地时区。为此,您需要自己格式化字符串:
There only is a .toISOString()
method, but that will not use the local timezone. For that, you will need to format the string yourself:
function toLocaleISOString(date) {
function pad(n) { return ("0"+n).substr(-2); }
var day = [date.getFullYear(), pad(date.getMonth()+1), pad(date.getDate())].join("-"),
time = [date.getHours(), date.getMinutes(), date.getSeconds()].map(pad).join(":");
if (date.getMilliseconds())
time += "."+date.getMilliseconds();
var o = date.getTimezoneOffset(),
h = Math.floor(Math.abs(o)/60),
m = Math.abs(o) % 60,
o = o==0 ? "Z" : (o<0 ? "+" : "-") + pad(h) + ":" + pad(m);
return day+"T"+time+o;
}
这篇关于如何在javascript中将ISOString转换为本地ISOString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!