If I have a JSON DateTime:
"DateCreated":"\/Date(1301692095627)\/"
How can I format that into a 12 hour time using jQuery?
Right now I’m using this:
function DateDeserialize(dateStr) {
return eval('new' + dateStr.replace(/\//g, ' '));
}
Which outputs the JSON object as:
Fri Apr 01 2011 17:08:15 GMT-0400 (Eastern Daylight Time)
But I would like to get just the time, in 12 hour format.
Use the
.getHours()method on the date object (which returns 0-23). Check to see if it’s over 12 hours, if so, subtract 12 hours otherwise it’s already fine.see this jsFiddle: http://jsfiddle.net/s2hyL/1/
If you need to perform formatting (or complex parsing), I highly recommend the date.js library by Matt Kruse.