scatman
scatman

Reputation: 14555

how can i convert a date in javascript to string

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?

Upvotes: 1

Views: 304

Answers (2)

Sam
Sam

Reputation: 840

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 ?

Upvotes: 2

YOU
YOU

Reputation: 123791

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")

Upvotes: 2

Related Questions