views:

754

answers:

4

This is probably something easy, but I'm a bit confused how to do this. How can I, using JavaScript, parse only the time from the following ISO 8601 date string:

 2009-12-06T17:10:00

In other words, with the string above, I'd like to output:

5:10 PM

Any guidance/tutorials on this would be great.

A: 

string.slice(11,16) will return 17:10. From there (possibly using slice in more interesting and exciting manners) it should be fairly simple to get it into 24-hour format.

me_and
+2  A: 

Similar questions have been asked here before. See what-is-the-best-way-to-parse-a-time-into-a-date-object-from-user-input-in-javasc, for example. Or just search for "javascript parse time" and such.

Peter Hansen
You're not supposed to answer like this. You're supposed to close the question as an exact duplicate and point it to the correct question. This way, there won't be a bunch of dupe questions polluting the database.
Josh Stodola
@Josh, thanks for the pointer. I didn't think someone with reputation 176 could close anything. Also, it would be more helpful to point me and others to the instructions where it would teach us about this sort of thing. Lastly, I didn't think it was an "exact" duplicate... if you do, feel free to close it yourself, as you appear to have the required reputation.
Peter Hansen
A: 

This is so called ISO 8601 format. Mochikit comes with a function to parse it,

http://mochikit.com/doc/html/MochiKit/DateTime.html

You can get a Date object like this,

  timestamp = isoTimestamp("2009-12-06T17:10:00");

Just copy the function if you don't want use Mochikit.

ZZ Coder
A: 

Parsing the ISO timestamp is easy, formatting the time in a culturally appropriate way is hard (5:10 PM is not appropriate for all locales) Many toolkits provide routines for the ISO part, and it's even part of the new ECMAScript 5 standard; only a couple do the latter part, however.

You can try dojo.date.stamp.fromISOString and dojo.date.locale.format.

I believe Date-JS can format times also.

peller