views:

1327

answers:

6

I noticed that javascript new 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.

+2  A: 

i love this link 10 example :

http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3

and this

http://www.elated.com/articles/working-with-dates/

Haim Evgi
Good resource. Thanks for the link! +1
Cerebrus
your welcome :) .It's my pleasure
Haim Evgi
+1  A: 

Make sure you checkout datejs when dealing with dates in JavaScript. It's quite impressive and well documented as you can see in case of the toString function.

Tim Büthe
A: 

Just another option, which I wrote:

DP_DateExtensions Library

Not sure if it'll help, but I've found it useful in several projects - looks like it'll do what you need.

Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc. It's liberally open sourced.

No reason to consider it if you're already using a framework (they're all capable), but if you just need to quickly add date manipulation to a project give it a chance.

Jim Davis
+1  A: 

The functionality you cite is not standard Javascript, not likely to be portable across browsers and therefore not good practice. The ECMAScript 3 spec leaves the parse and output formats function up to the Javascript implementation. ECMAScript 5 adds a subset of ISO8601 support. I believe the toString() function you mention is an innovation in one browser (Mozilla?)

Several libraries provide routines to parameterize this, some with extensive localization support. You can also check out the methods in dojo.date.locale.

peller
A: 

It may appear that jQuery or some other library have extended your Date object with advanced version of toString method. In my case it was jQuery.

Vasili
A: 

Formate date in JavaScript

http://java.pakcarid.com/Cpp.aspx?sub=389&ff=3078&topid=40&sls=25

lois