1

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 Answers 2

2
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")
Sign up to request clarification or add additional context in comments.

3 Comments

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?
That is by design actually, month starts from 0 (Zero), more info here
The month is 0 indexed. 0 is January. 11 is December. It's just the way it is :)
2

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 ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.