date - Use JavaScript to Parse Time -


This is probably something simple, but I have a little confused how to do this. Java Script, how can I parse only the time from the following date string:

  2009-12-06T17: 10: 00  

In other words With the above string, I want to output:

  5:10 PM  

Any guidance / tutorials on this would be great.

Chrome & amp; Firefox: Standard Javascript Date Constructor ISO 8601 takes the date string. For example:

var sampleDate = new date ("2010-03-07T02: 13: 46Z");

The date returns the item: "Sun March 07 2010 13:13:46 GMT + 1100 (Australian Eastern Daylight Time)" This does not work in IE (latest IE 9 Including:)

Here is a cross-browser solution by Paul Sowden:

  Date.prototype.setISO8601 = function (string) {var regexp = "([0- 9 ] {4}) (- ([0-9] {2}) (- ([0- 9] {2}) "+" (t ([0- 9] {2}): ([0- 9 ] {2}) (: ([0- 9] {2}) (\. ([0-9] +))? "+" (Zed (([- +]) ([0- 9] {2}): ([0- 9] {2})))))) "??? Var d = string.match (new regExp (regexp)); Var Offset = 0; Var Date = New Date (D [1], 0, 1); If (d [3]) {date.setMonth (d [3] - 1); } If (d [5]) {date.setDate (d [5]); } If (d [7]) {date.setHours (d [7]); } If (d [8]) {date.setMinutes (d [8]); } If (d [10]) {date.setSeconds (D [10]); } If (d [12]) {date.setMilliseconds (number ("0" + d [12]) * 1000); } If (d [14]) {offset = (number (d [16]) * 60) + number (d [17]); Offset * = ((D [15] == '-')? 1: -1); } Offset - = date.getTimezoneOffset (); Time = (number (date) + (offset * 60 * 1000)); This.setTime (number (time)); }  

Usage:

  var date = new date (); Date.setISO8601 ("2005-03-26T19: 51: 34Z");  

If you manipulate javascript from time to time, then I can also suggest some JS libraries to check. It handles some common things like date parsing, formatting and counting of differences between two dates and support for many localization.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -