0

I'm trying to convert a date in javascript from MM/dd/yyyy to yyyy/MM/dd

so this works:

var d = new Date("08/08/2012");
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);

output = 2012/7/8

///////////////////////////////////////////////////

this does not:

var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);

and neither does this:

var dateString = "08/08/2012";
var d = Date.parse(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);

how do I make it work with a string variable? thanks

~Myy

3
  • What does the browser's console log when it tries to execute the code that doesn't work? Commented Aug 9, 2012 at 20:22
  • Are you saying that you want 08/08/2012 to print as 2012/7/8 (i.e., with the month changed from 08 to 7)? Commented Feb 28, 2013 at 0:35
  • no, I was having a problem using a string variable vs the new Date() function. Commented Mar 2, 2013 at 17:22

3 Answers 3

5
var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);

That should, and does, work. Keep in mind that JavaScript stores months as a zero-indexed value.

If you want to have leading zeros, then you'll have to do some magic:

var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + ('0' + (d.getMonth()+1)).slice(-2) + "/" + ('0' + d.getDate()).slice(-2);
document.write(dateString);​

jsFiddle

The reason why your Date.parse( ) example is not working, is because that function returns a timestamp (number of milliseconds since 1970), instead of a Date object. Therefore, you can't call functions like getFullYear() on the timestamp.

Sign up to request clarification or add additional context in comments.

4 Comments

The million dollar question is why Date.parse doesn't work.
@Nick Date.parse does work, but it doesn't return a Date object. It returns a timestamp.
I swear I tried the second method time after time, and it didn't work, and now that I tried again it worked. I guess I was missing something, Thanks for the quick response!
@MrSlayer I really wish they would have named the function better.
4

If all you need to do is re-order the values, you can do:

var dateString = "08/08/2012";
var dateElements = dateString.split("/");
var outputDateString = dateElements[2] + "/" + dateElements[0] + "/" + dateElements[1];
document.write(outputDateString );

Comments

1

I can confirm with MrSlayer that the code works in jsFiddle.

Your attempts at using Date.parse() should actuall be using Date(String(dateString)).

Don't forget to add 1 for each month.

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.