Billy Moon Billy Moon - 2 months ago 19
Javascript Question

Javascript date format like ISO but local

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)

Answer

AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:

var date = new Date();
var year = date.getFullYear();
var month = date......


var ISOdate = year + "-" + month + "-" + .... ;

But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)

EDIT:

Moment.js seems the thing you're looking for: http://momentjs.com/

Comments