views:

41

answers:

2

i have a javascript method that takes a date:

  convert(new Date("02/20/2010");

how can i let convert return "02/20/2010" as a string?

+2  A: 
d=new Date("02/20/2010");

(d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear();

2/20/2010

or just print it without passing it to Date constructor?

alert("02/20/2010")
S.Mark
yeah i tried this , and i noticed that the month is always giving me an answer < the actual month by 1!! y is that? y did u put +1 to the month?
scatman
That is by design actually, month starts from 0 (Zero), more info [here](http://www.w3schools.com/jsref/jsref_getmonth.asp "w3schools Date getMonth")
S.Mark
The month is 0 indexed. 0 is January. 11 is December. It's just the way it is :)
Matt
aaa i c.. thx:)
scatman
A: 

The output of Date("02/20/2010") is something like Thu Apr 22 2010 15:15:51 GMT+0530 (India Standard Time) which by itself is string.

There are some in-built Date/Time manipulation functions which might be of use to you

toDateString() method

d=new Date("02/20/2010");
d.toDateString();
==> Tue Feb 02 2010

d.toUTCString() => Fri, 19 Feb 2010 18:30:00 GMT

But if "02/20/2010" is what you want as output, you can go with the above answers.

By the way why do you want a method that gives out the output same as the input ?

Shoaib