How to convert ISOString to local ISOString in javascript?

How to convert ISOString to local ISOString in javascript?

I have an ISO 8086 style string (e.g. '2013-02-18T16: 39: 17 + 00: 00')

And I want to convert this to a local style string of ISO_8601 ...

'2013-02-18T16:39:17+00:00' -> '2013-02-19T01:39:17+09:00' 

What should I do?

-one
javascript
Feb 18 '13 at 17:10
source share
1 answer

There is only a .toISOString() method , but it will not use the local time zone. To do this, you 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; } 
+1
Feb 18 '13 at 17:38
source share



All Articles