4

How to format this:

/Date(1292962456255)/

as regular looking date in JavaScript/jQuery?

5
  • What kind of a date is this? A timestamp? Commented Dec 21, 2010 at 20:19
  • Comes back to Json like this from a c3 Datetime object Commented Dec 21, 2010 at 20:20
  • 2
    what exactly is the expected output of this date? Commented Dec 21, 2010 at 20:20
  • I really don't know. For some reason passing a c# datetime through json to the view comes back like this after I perform a .toString() on it Commented Dec 21, 2010 at 20:22
  • @John -- maybe? i really dont know Commented Dec 21, 2010 at 20:23

4 Answers 4

4

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;
            }
Sign up to request clarification or add additional context in comments.

5 Comments

This isn't valid JavaScript, there should be a = instead of a : on the first line -- and what's with the return msEncodedDate? If the string doesn't match the pattern, return the function itself? That doesn't make any sense at all.
@Theo your right about the equal. but your second observation is wrong. The function will return the a JS date if the string matches the regex. otherwise return back the original value passed in. Clearly you could modify this to return an error code or null.
Ah, sorry, I misread and took the argument name for the function name. It's still a strange thing to do, I would be very surprised to get a string back when I expect a date. Returning null or raising an exception would be much more appropriate.
@Theo I would hope that C# is not passing invalid dates through JSON
You can never trust that what you get will always be correct.
2

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.

Comments

1

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.

3 Comments

Dont forget the radix on parseInt
Radix is not needed since it defaults to base 10.
that is not actually correct. Radix does not default to base 10. "If no radix argument is provided or if it is assigned a value of 0, the function tries to determine the base. If the string starts with a 1-9, it will be parsed as base 10. If the string starts with 0x or 0X it will be parsed as a hexidecimal number. If the string starts with a 0 it will be parsed as an octal number. (Note that just because a number starts with a zero, it does not mean that it is really octal.) "
-2

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

1 Comment

He asked for code that would format the date in the string, not what it was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.