Reputation: 24082
How to format this:
/Date(1292962456255)/
as regular looking date in JavaScript/jQuery?
Upvotes: 4
Views: 2881
Reputation: 46557
Check out moment.js! It's "A lightweight javascript date library for parsing, manipulating, and formatting dates". It is a really powerful little library.
Here's an example...
var today = moment(new Date());
today.format("MMMM D, YYYY h:m A"); // outputs "April 11, 2012 2:32 PM"
// in one line...
moment().format("MMMM D, YYYY h:m A"); // outputs "April 11, 2012 2:32 PM"
Here's another example...
var a = moment([2012, 2, 12, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, March 12th 2012, 3:25:50 pm"
a.format("ddd, hA"); // "Mon, 3PM"
a.format("D/M/YYYY"); // "12/3/2012"
Also, its worth mentioning to checkout date.js. I think the two libraries complement each other.
Upvotes: 2
Reputation: 132942
The number is a timestamp with millisecond resolution. This number can be passed to JavaScript's Date
class' constructor. All that is needed is some code to extract it from the string:
var dateString = "/Date(1292962456255)/";
var matches = dateString.match(/^\/Date\((\d+)\)\/$/);
var date = new Date(parseInt(matches[1], 10));
The regexp on the second line gets a bit messy since the string contains /, ( and ) at precisely the positions that they are needed in the regexp (are you sure what you have is strings that look like that, and not a description of a pattern that would extract them?).
Another way of doing it is to use eval
:
var dateString = "/Date(1292962456255)/";
var date = eval("new " + dateString.substring(1, dateString.length - 1));
but that may open up for an XSS attack, so I don't recommend it.
Upvotes: 1
Reputation: 86892
This is what I call an "Microsoft Date" and the following function will convert the encoded date to a javascript date time
var msDateToJSDate = function(msDate) {
var dtE = /^\/Date\((-?[0-9]+)\)\/$/.exec(msDate);
if (dtE) {
var dt = new Date(parseInt(dtE[1], 10));
return dt;
}
return null;
}
Upvotes: 4
Reputation: 2325
I think this a microtime
. Similar to PHP's microtime function. Or in new Date().getTime()
in JavaScript.
// PHP
$ php -r "var_dump(microtime(true));"
float(1292963152.1249)
// JavaScript
new Date().getTime()
1292963411830
Upvotes: -2