96

Possible Duplicate:
How to format a JSON date?

I have the following result from a $getJSON call from JavaScript. How do I convert the start property to a proper date in JavaScript?

[ {"id":1,"start":"/Date(1238540400000)/"}, {"id":2,"start":"/Date(1238626800000)/"} ]

Thanks!

3
  • "1238540400000" what's this? Milliseconds since the year 1970? Commented Aug 7, 2009 at 10:49
  • 1
    @Meeh : yup, the number milliseconds since 1970/01/01 Commented Aug 7, 2009 at 11:05
  • You could use JSON++ instead of JSON. JSON++ is the same than JSON but with support for JavaScript types such as Date. Commented Nov 14, 2018 at 11:06

4 Answers 4

104

You need to extract the number from the string, and pass it into the Date constructor:

var x = [{ "id": 1, "start": "\/Date(1238540400000)\/" }, { "id": 2, "start": "\/Date(1238626800000)\/" }]; var myDate = new Date(x[0].start.match(/\d+/)[0] * 1); 

The parts are:

x[0].start - get the string from the JSON x[0].start.match(/\d+/)[0] - extract the numeric part x[0].start.match(/\d+/)[0] * 1 - convert it to a numeric type new Date(x[0].start.match(/\d+/)[0] * 1)) - Create a date object 
Sign up to request clarification or add additional context in comments.

2 Comments

Don't * 1 to convert a string to a number. Use parseInt(number, 10). Also, if you want a cool trick like * 1, just try +str to make it a number.
@Greg I want a short date instead of "Thu Apr 26 2018 14:39:28 GMT+0700 (SE Asia Standard Time)"
100

I use this:

function parseJsonDate(jsonDateString){ return new Date(parseInt(jsonDateString.replace('/Date(', ''))); } 

Update 2018:

This is an old question. Instead of still using this old non standard serialization format I would recommend to modify the server code to return better format for date. Either an ISO string containing time zone information, or only the milliseconds. If you use only the milliseconds for transport it should be UTC on server and client.

  • 2018-07-31T11:56:48Z - ISO string can be parsed using new Date("2018-07-31T11:56:48Z") and obtained from a Date object using dateObject.toISOString()
  • 1533038208000 - milliseconds since midnight January 1, 1970, UTC - can be parsed using new Date(1533038208000) and obtained from a Date object using dateObject.getTime()

7 Comments

This wouldn't account for the closing ')/'
The parseInt function parses only until it founds legal characters for int, then it stops. The closing ')/' will not be parsed.
Cool, but that doesn't sound very robust:P
It is robust, in that it works every time, in all situations
@J.T.Taylor Yes indeed, I just wanted to mitigate your affirmation : "it works every time, in all situations"
|
11

If you use jQuery

In case you use jQuery on the client side, you may be interested in this blog post that provides code how to globally extend jQuery's $.parseJSON() function to automatically convert dates for you.

You don't have to change existing code in case of adding this code. It doesn't affect existing calls to $.parseJSON(), but if you start using $.parseJSON(data, true), dates in data string will be automatically converted to Javascript dates.

It supports Asp.net date strings: /Date(2934612301)/ as well as ISO strings 2010-01-01T12_34_56-789Z. The first one is most common for most used back-end web platform, the second one is used by native browser JSON support (as well as other JSON client side libraries like json2.js).

Anyway. Head over to blog post to get the code. http://erraticdev.blogspot.com/2010/12/converting-dates-in-json-strings-using.html

1 Comment

thanks for the blog... I think you need to add "Z?" to the regex for matching the date otherwise we fail to match non-UTC dates.
8

If that number represents milliseconds, use the Date's constructor :

var myDate = new Date(1238540400000); 

1 Comment

Actually more like myDate = new DateTime(1970, 1, 1).AddMilliseconds(jsonDateVal);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.