Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  96] [ 4]  / answers: 1 / hits: 28621  / 12 Years ago, thu, september 13, 2012, 12:00:00

how do I format a javascript date like ISO format, but in local time?



with myDate.toISOString() I am getting the time as: 2012-09-13T19:12:23.826Z



but here, it is 22:13, so how do I include the timezone in above format?






I ended up doing...



pad=function(e,t,n){n=n||0,t=t||2;while((+e).length<t)e=n+e;return e}
c = new Date()
c.getFullYear()+-+pad(c.getMonth()+1)+-+pad(c.getDate()-5)+T+c.toLocaleTimeString().replace(/D/g,':')+.+pad(c.getMilliseconds(),3)

More From » javascript

 Answers
30

A bit of a hack but can be done in one line by taking advantage of the fact that Sweden uses a format very close to ISO:


// Returns a string like 2021-01-17T01:59:57
function dateToISOButLocal(date) {
return date.toLocaleString('sv').replace(' ', 'T');
}



To support milliseconds:


return date.toLocaleString('sv', {year:'numeric', month:'numeric', day:'numeric', hour:'numeric', minute:'numeric', second:'numeric', fractionalSecondDigits: 3}).replace(',', '.').replace(' ', 'T');

[#83094] Wednesday, September 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austynp

Total Points: 505
Total Questions: 118
Total Answers: 106

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
austynp questions
;