3

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.

2
  • probably a duplicate in some capacity or another... couldn't find anything upon first search Commented Feb 3, 2010 at 18:21
  • This is best solved with datetime parsing and formatting, not string manipulation as the tag suggests. Commented Feb 3, 2010 at 18:22

4 Answers 4

4

How about:

s.substr(5,2) + '/' + s.substr(8) + '/' + s.substr(0,4) 
Sign up to request clarification or add additional context in comments.

1 Comment

@cjstehno: I also swapped the day and month, but it's fixed now. :)
1

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.

Comments

1

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.

6 Comments

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
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 -- 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. :-)
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.
It's still not exactly what Derek wants as getMonth() and getDate() returns a single digit when <10.
|
1

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.