views:

202

answers:

4

The best way to take a string that is formated like...

YYYY-MM-DD

and make it appear like...

MM/DD/YYYY

The reason it is not a javascript date object is because I am working with massive amounts of data and these dates are being pulled from a database.

I see no need to convert it to a date object.

+3  A: 

How about:

s.substr(5,2) + '/' + s.substr(8) + '/' + s.substr(0,4)
Guffa
nice one-liner. :-)
cjstehno
@cjstehno: I also swapped the day and month, but it's fixed now. :)
Guffa
+1 and the accept for brevity
Derek Adair
+1  A: 

By brute force you could do something like:

var old = '2010-02-03'.split('-');
var desired = old[1] + '/' + old[2] + '/' + old[0];

Saves the hassle of working with Date object.

cjstehno
Not to be overly pedantic, but this will not work. `new` is a reserved word in Javascript. You'll have to use a different variable name.
ghoppe
lol... I fixed it, good catch. :-)
cjstehno
+1  A: 

You can use a regular expression in JavaScript (assuming JS because your question is tagged as such):

var date = "2010-05-09";
var formatted = date.replace(/([0-9]{4})-([0-9]{2})-([0-9]{2})/, "$2/$3/$1")

What I like about this more than using substring is that it seems more apparent what is being done.

Vivin Paliath
+1  A: 

If you replace the dashes with slashes it will parse, then you can use the date functions to get the various components (or convert to string using one of the various toString() functions).

var date = new Date( Date.parse( old.replace(/-/g,'/') ) );
alert( date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear() );

This has the advantage of being able to use the date as a date for calculations, not merely doing string formatting. If string formatting is all you need AND your date strings are always valid, then using @Guffa's substr method is probably the best way to handle it.

tvanfosson
The dates have no functionality and are always valid (they are inserted as a timestamp via mysql)... so at least I hope mysql's datestamp format would be valid!! lol
Derek Adair
Actually, in my testing, vivin's regular expression method is ~20% faster. The slowest method is cjstehno's split into array and combine method.
ghoppe
@ghoppe -- ok, but I would submit to you that "faster" is not always equivalent to "better". Readability is also a concern. Depending on your regular expression foo, the regex solution may simply be incomprehensible whereas anyone ought to be able to figure out substr. I actually find mine to be the most comprehensible even though it's probably the slowest. Anyone who can't see immediately what it's doing ought not be programming. :-)
tvanfosson
I fixed some issues with parse() returning ms, not an actual Date. The replacement (unlike C#) needs to use a regex and specify global replacement. The solution as it stands now has been tested.
tvanfosson
It's still not exactly what Derek wants as getMonth() and getDate() returns a single digit when <10.
ghoppe
Oh and by the way, it's surprisingly fast, actually beating out the .split into array method on my browser test. Still, will slow down a bit if Derek needs the 0 padding. :)
ghoppe