149

I have a following piece of JSON:

\/Date(1293034567877)\/

which is a result of this .NET code:

var obj = DateTime.Now;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.Serialize(obj).Dump();

Now the problem I am facing is how to create a Date object from this in JavaScript. All I could find was incredible regex solution (many containing bugs).

It is hard to believe there is no elegant solution as this is all in JavaScrip, I mean JavaScript code trying to read JSON (JavaScript Object Notation) which is supposed to be a JavaScript code and at this moment it turns out it's not cause JavaScript cannot do a good job here.

I've also seen some eval solutions which I could not make to work (besides being pointed out as security threat).

Is there really no way to do it in an elegant way?

Similar question with no real answer:
How to parse ASP.NET JSON Date format with GWT

Community
  • 1
  • 1
Piotr Owsiak
  • 6,081
  • 8
  • 39
  • 42

17 Answers17

161

The JSON.parse function accepts an optional DateTime reviver function. You can use a function like this:

dateTimeReviver = function (key, value) {
    var a;
    if (typeof value === 'string') {
        a = /\/Date\((\d*)\)\//.exec(value);
        if (a) {
            return new Date(+a[1]);
        }
    }
    return value;
}

Then call

JSON.parse(somejsonstring, dateTimeReviver);

And your dates will come out right.

Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
Tim
  • 1,629
  • 2
  • 10
  • 2
  • 7
    This practice of encoding non-primitive typed data in a primitive type (string) is insane. Encode dates in a JSON object with meaningful properties, or to go even further, include a "$type" property in the JSON object so that the parse/deserialize routine can revive the type appropriately and even use custom converters if you want to pack all the information into a single property value like "ticks" or "ms_since_epoch". – Triynko Dec 02 '13 at 21:10
  • 7
    I had to modify the regex like this /\/Date\((-?\d*)\)\// so that it would be able to handle negative numbers as well. Negative numbers appear when you have a very old DateTime (before the Epoch) that was converted by .NET to JSON. – Jason Parker Jan 30 '14 at 21:37
  • @ClearCloud8: You miss back slashes: /\/Date\\((-?\d*)\\)\// – Jerther Oct 31 '14 at 12:55
  • 1
    I never knew about this function - this is so useful! – keldar Feb 23 '16 at 10:48
  • 2
    Why not `a = Date.parse(value)` instead of `/\/Date\((\d*)\)\//.exec(value);`? – Red Sep 13 '18 at 22:22
60

There is no standard JSON representation of dates. You should do what @jAndy suggested and not serialize a DateTime at all; just send an RFC 1123 date string ToString("r") or a seconds-from-Unix-epoch number, or something else that you can use in the JavaScript to construct a Date.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • 3
    Thanks I was going down a dead path, you were the first to point out that JSON does not support Date type. – Piotr Owsiak Dec 22 '10 at 18:01
  • 3
    JSON supports numbers, strings, objects, arrays, and the literals true, false, and null. Since Date is none of those, it is a complex type which should be stored as an object, rather than a string, so you can include type information such as the type name in special members like "$type" that would never resolve to a real object member. Such meta-members can be used to revive the JSON object to a strongly-typed runtime object later. I think the practice of sticking a date in a string is stupid, because it unnecessarily creates reserved string patterns and tries to match them on every string. – Triynko Dec 02 '13 at 22:31
  • 7
    There is now a standard JSON date format. https://tools.ietf.org/html/rfc7493#section-4.3 – Bryan Larsen Feb 06 '17 at 14:18
51

This answer from Roy Tinker here:

var date = new Date(parseInt(jsonDate.substr(6)));

As he says: The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.

Another option is to simply format your information properly on the ASP side such that JavaScript can easily read it. Consider doing this for your dates:

DateTime.Now()

Which should return a format like this:

7/22/2008 12:11:04 PM

If you pass this into a JavaScript Date constructor like this:

var date = new Date('7/22/2008 12:11:04 PM');

The variable date now holds this value:

Tue Jul 22 2008 12:11:04 GMT-0700 (Pacific Daylight Time)

Naturally, you can format this DateTime object into whatever sort of string/int the JS Date constructor accepts.

Community
  • 1
  • 1
treeface
  • 13,270
  • 4
  • 51
  • 57
  • Thanks treeface, this answer helped me out with something recently! – Malice Nov 10 '11 at 21:22
  • 4
    Never, ever, rely on default date<->string conversion formats. Using milliseconds since Epoch, which stays in the numeric-types domain, is far more simple and reliable. – Johan Boulé Sep 12 '12 at 14:29
  • 3
    This answer presents two solutions - the first one is correct (the parseInt) and the second one wrong, so not sure whether to upvote or downvote! The problem with simply outputting as a string is the date can easily flip backwards if the server is in one country eg US and the browser in another eg UK. – mike nelson May 16 '13 at 05:25
  • The first answer to give me any kind of clue – Nick.Mc Apr 10 '16 at 08:03
  • 1
    An OK answer until "*Consider doing this for your dates…*". Suggesting a non–standard format that introduces implementation dependent parsing and timezone issues is not a good idea. The OP format is preferable (though not ideal). – RobG May 17 '19 at 09:56
34

If you use the JavaScript style ISO-8601 date in JSON, you could use this, from MDN:

const jsonDate = (new Date()).toJSON();
const backToDate = new Date(jsonDate);
console.log(jsonDate); //2015-10-26T07:46:36.611Z

To create an ISO-8601 date string:

myDate.toISOString()
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
  • 2
    imo this is the most elegant answer and should be the accepted one. – John Jun 08 '16 at 23:21
  • 1
    Very elegant indeed, but this does not relate to the specific date format that was mentioned in the question. – asiop Sep 14 '16 at 16:41
11

You can convert JSON Date to normal date format in JavaScript.

var date = new Date(parseInt(jsonDate.substr(6)));
Pang
  • 9,564
  • 146
  • 81
  • 122
ViPuL5
  • 613
  • 7
  • 9
6

What's wrong with:

new Date(1293034567877);

This returns for me "Wed Dec 22 2010 16:16:07 GMT+0000 (GMT Standard Time)".

Or do you need to get the number out the json?

Psytronic
  • 6,043
  • 5
  • 37
  • 56
  • 4
    What is wrong with your solution? Well the 1293034567877 is not the JSON I have, right? Also I don't need to get the number out the JSON, I need to get the Date out the JSON. I'm expecting a little bit more from JavaScript than just being able to to everything with regex. I need my code to be readable and not look like a cartoon curse. – Piotr Owsiak Dec 22 '10 at 17:36
  • 8
    I'd blame .NET for producing a serialization of a date object in a format as weird as `\/Date(1293034567877)\/`. If it was sane, it would just output epoch time and you could initialize a Date object with that. – Quentin Dec 22 '10 at 17:39
  • I did this like Psytronic offers. The matter is that this is not JSON date, this is .NET date. It is not a great problem to parse a number out of the string, is it? – Michael Sagalovich Dec 22 '10 at 17:40
  • @David Dorward: that might be true, what would be a valid JSON in this case? – Piotr Owsiak Dec 22 '10 at 17:41
  • JSON doesn't have a Data data type. As I said, I would just use the epoch time, as a Number. `{ date: 1293034567877 }`. Then you could inflate it into a Date object in JS, `foo.date = new Date(foo.date);` – Quentin Dec 22 '10 at 17:47
  • @David Dorward: I just learned that from google. What a huge mistake in JSON! – Piotr Owsiak Dec 22 '10 at 17:58
  • A Number is all you need to represent a date in JSON. There benefits brought by an extra data type wouldn't outweigh the costs of the added complexity. – Quentin Dec 22 '10 at 18:02
  • @Piotr "A huge mistake" it is not. You can represent a Date as a number or string in multiple ways that make sense to nearly all languages, including epoch time or the UNIX timestamp. – treeface Dec 22 '10 at 18:04
  • @treeface: well if you consider the fact that JSON stands for JavaScript Object Notation it becomes obvious that someone made a mistake, either naming this thing or forgetting to include the Date type. – Piotr Owsiak Dec 27 '10 at 14:33
  • @Piotr But JavaScript Object Notation isn't JavaScript, it is simply a syntax convention taken largely from JavaScript. What point would there be to include a `Date` type when it can be represented as a `String`? – treeface Dec 27 '10 at 17:30
  • 2
    @treeface: If JSON is not JavaScript then I think tutorials and books are to be blamed for this common misunderstanding. Anyway I gladly stand corrected, really. As for your suggestion that Date can be represented as String I can say that everything can be represented as String, right? But this would not make our job easier, but terribly painfull and hellish. I guess my problem stems from the fact that I considered JSON as a serialization format (advertised to take less bandwidth and work better with JavaScript than XML). As it turns out it's not, at least not a painless one. – Piotr Owsiak Jan 18 '11 at 10:10
  • 1
    @treeface: I did google your claim about JSON and found out that JSON is JavaScript, actually it is a subset of JavaScript. See RFC #4627 "The application/json Media Type for JavaScript Object Notation (JSON)" and look for a statement: "JSON's design goals were for it to be minimal, portable, textual, and a subset of JavaScript.". Now when I think about it, it seems obvious as you can call eval() on JSON. – Piotr Owsiak Jan 18 '11 at 10:27
  • 1
    @David Dorward: I would rather have the added complexity be implemented deep inside libraries (either .NET, Java, Ruby, Python, or whatevery language/platform you are on) rather than left the detail to be handled by programmer. Also note that you don't need boolean and integer data types support in JSON, you can just put them in strings, right? Can you imagine how awfull that would be to get anythong from JSON then? – Piotr Owsiak Jan 18 '11 at 10:33
2

I know this is a very old thread but I wish to post this to help those who bump into this like I did.

if you don't care about using a 3rd party script, you can use moment,js Then you can use .format() to format it to anything you want it to.

Eman
  • 1,093
  • 2
  • 26
  • 49
2

Dates are always a nightmare. Answering your old question, perhaps this is the most elegant way:

eval(("new " + "/Date(1455418800000)/").replace(/\//g,""))

With eval we convert our string to javascript code. Then we remove the "/", into the replace function is a regular expression. As we start with new then our sentences will excecute this:

new Date(1455418800000)

Now, one thing I started using long time ago, is long values that are represented in ticks... why? well, localization and stop thinking in how is date configured in every server or every client. In fact, I use it too in databases.

Perhaps is quite late for this answer, but can help anybody arround here.

1

The answer to this question is, use nuget to obtain JSON.NET then use this inside your JsonResult method:

JsonConvert.SerializeObject(/* JSON OBJECT TO SEND TO VIEW */);

inside your view simple do this in javascript:

JSON.parse(/* Converted JSON object */)

If it is an ajax call:

var request = $.ajax({ url: "@Url.Action("SomeAjaxAction", "SomeController")", dataType: "json"});
request.done(function (data, result) { var safe = JSON.parse(data); var date = new Date(safe.date); });

Once JSON.parse has been called, you can put the JSON date into a new Date instance because JsonConvert creates a proper ISO time instance

Callum Linington
  • 14,213
  • 12
  • 75
  • 154
1

AngularJS couldn't parse .NET JSON date /Date(xxxxxxxxxxxxx)/ string either..

I side stepped this issue by formatting the date to its ISO 8601 string representation instead of dumping the Date object directly...

Here is a sample of ASP.NET MVC code..

return Json(new { 
  date : DateTime.Now.ToString("O") //ISO 8601 Angular understands this format
});

I tried RFC 1123 but it doesn't work.. Angular treats this as string instead of Date.

return Json(new { 
  date : DateTime.Now.ToString("R") //RFC 1123 Angular won't parse this
});
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
0
function parseJsonDate(jsonDate) {

    var fullDate = new Date(parseInt(jsonDate.substr(6)));
    var twoDigitMonth = (fullDate.getMonth() + 1) + ""; if (twoDigitMonth.length == 1) twoDigitMonth = "0" + twoDigitMonth;

    var twoDigitDate = fullDate.getDate() + ""; if (twoDigitDate.length == 1) twoDigitDate = "0" + twoDigitDate;
    var currentDate = twoDigitMonth + "/" + twoDigitDate + "/" + fullDate.getFullYear();

    return currentDate;
};
Muzafar
  • 21
  • 4
0

As Callum mentioned, for me, the best way is to change the Controller method to string instead of JsonResult".

public string GetValues()
{
  MyObject.DateFrom = DateTime.Now;
  return JsonConvert.SerializeObject(MyObject);
}

From the ajax method you can do something like this

 $.ajax({
 url: "/MyController/GetValues",
 type: "post",
 success: function (data) {
 var validData = JSON.parse(data);
//if you are using datepicker and you want set a format
$("#DateFrom").val($.datepicker.formatDate("dd/mm/yy", new Date(validData.DateFrom)));                                      
// if you want the date as returned
$("#DateFrom").val(new Date(validData.DateFrom))
}
});
Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
onixpam
  • 1
  • 1
0

using eval function works just have to remove the forward slash at front and back.

var date1 = "/Date(25200000)/"
eval("new " + date1.substring(1, date1.length - 1));

yields Thu Jan 01 1970 00:00:00 GMT-0700 (US Mountain Standard Time)

vernmic
  • 25
  • 5
0

I've not used .Net for things like this. If you were able to get it to print something like the following out it should work.

Note, unless you're parsing that JSON string by some other means or only expect users to have modern browers with a built in JSON parser you need to use a JS framework or JSON2 to parse the JSON string outputted by the server into a real JSON object.

// JSON received from server is in string format
var jsonString = '{"date":1251877601000}';

//use JSON2 or some JS library to parse the string
var jsonObject =  JSON.parse( jsonString );

//now you have your date!
alert( new Date(jsonObject.date) );

Wiki Link

Modern browsers, such as Firefox 3.5 and Internet Explorer 8, include special features for parsing JSON. As native browser support is more efficient and secure than eval(), it is expected that native JSON support will be included in the next ECMAScript standard.[6]


Link to JSON2 file

Live Example

subhaze
  • 8,815
  • 2
  • 30
  • 33
  • 1
    I get it, but my issue with JSON and Date type is that I need to explicitly do "new Date(" which is a) extra work b) additional piece of knowledge that needs to be communicated to the consumer. I am really disappointed to find out how this is handled and I basicaly consider it a mistake in JSON spec. – Piotr Owsiak Dec 27 '10 at 14:38
  • the whole web "development" world is basically a big shameless scam. they havent progressed since java script was introduced in 1995.. all web "developers" are cavemen. and they shoot everything which endangers their ecosystem. ie all innovations and innovators. – Boppity Bop Jul 30 '21 at 18:56
0

I ran into an issue with external API providing dates in this format, some times even with UTC difference info like /Date(123232313131+1000)/. I was able to turn it js Date object with following code

var val = '/Date(123232311-1000)/';
var pattern = /^\/Date\([0-9]+((\+|\-)[0-9]+)?\)\/$/;
var date = null;

// Check that the value matches /Date(123232311-1000)/ format
if (pattern.test(val)) {
  var number = val.replace('/Date(', '',).replace(')/', '');
  if (number.indexOf('+') >= 0) {
    var split = number.split('+');
    number = parseInt(split[0]) + parseInt(split[1]);
  } else if (number.indexOf('-') >= 0) {
    var split = number.split('-');
    number = parseInt(split[0]) - parseInt(split[1]);
  } else {
    number = parseInt(number);
    date = new Date(number);
  }
}
Martin Vich
  • 1,062
  • 1
  • 7
  • 22
-1
//
// formats a .net date into a javascript compatible date
//
function FormatJsonDate(jsonDt) 
{              
    var MIN_DATE = -62135578800000; // const

    var date = new Date(parseInt(jsonDt.substr(6, jsonDt.length-8)));                                                       
    return date.toString() == new Date(MIN_DATE).toString() ? "" : (date.getMonth() + 1) + "\\" + date.getDate() + "\\" + date.getFullYear(); 
}
sunil
  • 1
-1
function parseJsonDate(jsonDate) {

    var fullDate = new Date(parseInt(jsonDate.substr(6)));
    var twoDigitMonth = (fullDate.getMonth() + 1) + ""; if (twoDigitMonth.length == 1) twoDigitMonth = "0" + twoDigitMonth;

    var twoDigitDate = fullDate.getDate() + ""; if (twoDigitDate.length == 1) twoDigitDate = "0" + twoDigitDate;
    var currentDate = twoDigitMonth + "/" + twoDigitDate + "/" + fullDate.getFullYear();

    return currentDate;
};

//Use this function

var objDate=parseJsonDate("\/Date(1443812400000)\/");
alert(objDate);