Thursday, April 12, 2012

Formatting a date in javascript

I noticed that javascriptnew Date() function is very smart in accepting dates in several formats.



Xmas95 = new Date("25 Dec, 1995 23:15:00")
Xmas95 = new Date("2009 06 12,12:52:39")
Xmas95 = new Date("20 09 2006,12:52:39")


But i could not find documentation anywhere showing what all string formats that are valid while calling new Date() function.



This is for converting string to date.
Now, if we look at the opposite side that is converting date object to string.



Till now, I was under impression that javascript doesn't have inbuilt API to format date object into a string.
Today, i played toString() method on date object and surprisingly it serves the purpose of formatting date to strings.



var d1=new Date();
d1.toString('yyyy-MM-dd'); //returns "2009-06-29"
d1.toString('dddd, MMMM ,yyyy') //returns "Monday, June 29,2009"


But here also i could find any documentation on what all ways we can format the date object into a string.



can someone please point me to a documentation which lists about the format specifiers supported for new Date() object.





No comments:

Post a Comment