How to convert this date format: /Date(1268524800000)/?
Possible Duplicate:
How to format a JSON date?
I am calling a JSON web service via Javascript, and the StartDate
field is /Date(1268524800000)/
. How do I convert this to a human readable format?d
Try this:
var str = "/Date(1268524800000)/";
var num = parseInt(str.replace(/[^0-9]/g, ""));
var date = new Date(num);
alert(date);
Fiddle: http://jsfiddle.net/dS2hd/
You can use a regex to get the milliseconds, then use the Date constructor to get a Date object. Once you have your date object, you can do whatever you want with it.
var ms = parseInt("/Date(1268524800000)/".match(/((d+))/)[1]);
var d = new Date(ms);
alert(d.toString());
You can either eval()
it, or you can extract the number and pass it to a Date
constructor.
if (/^/Date((-?d+))/$/.test(val)) {
var serial = parseInt(RegExp.$1);
val = new Date(serial);
}
I've seen dates expressed as /Date(1234567890000-0500)/
, so a more robust procedure may be called for to handle the UTC offset.