I'm taking my first crack at Ajax with jQuery. I'm getting my data onto my page, but I'm having some trouble with the JSON data that is returned for Date data types. Basically, I'm getting a string back that looks like this:
/Date(1224043200000)/
From someone totally new to JSON - How do I format this to a short date format? Should this be handled somewhere in the jQuery code? I've tried the jQuery.UI.datepicker
plugin using $.datepicker.formatDate()
without any success.
FYI: Here's the solution I came up with using a combination of the answers here:
function getMismatch(id) {
$.getJSON("Main.aspx?Callback=GetMismatch",
{ MismatchId: id },
function (result) {
$("#AuthMerchId").text(result.AuthorizationMerchantId);
$("#SttlMerchId").text(result.SettlementMerchantId);
$("#CreateDate").text(formatJSONDate(Date(result.AppendDts)));
$("#ExpireDate").text(formatJSONDate(Date(result.ExpiresDts)));
$("#LastUpdate").text(formatJSONDate(Date(result.LastUpdateDts)));
$("#LastUpdatedBy").text(result.LastUpdateNt);
$("#ProcessIn").text(result.ProcessIn);
}
);
return false;
}
function formatJSONDate(jsonDate) {
var newDate = dateFormat(jsonDate, "mm/dd/yyyy");
return newDate;
}
This solution got my object from the callback method and displayed the dates on the page properly using the date format library.
Updated
We have an internal UI library that has to cope with both Microsoft's ASP.NET built-in JSON format, like
/Date(msecs)/
, asked about here originally, and most JSON's date format including JSON.NET's, like2014-06-22T00:00:00.0
. In addition we need to cope with oldIE's inability to cope with anything but 3 decimal places.We first detect what kind of date we're consuming, parse it into a normal JavaScript
Date
object, then format that out.1) Detect Microsoft Date format
2) Detect ISO date format
3) Parse MS date format:
4) Parse ISO date format.
We do at least have a way to be sure that we're dealing with standard ISO dates or ISO dates modified to always have three millisecond places (see above), so the code is different depending on the environment.
4a) Parse standard ISO Date format, cope with oldIE's issues:
4b) Parse ISO format with a fixed three millisecond decimal places - much easier:
5) Format it:
6) Tie it all together:
The below old answer is useful for tying this date formatting into jQuery's own JSON parsing so you get Date objects instead of strings, or if you're still stuck in jQuery <1.5 somehow.
Old Answer
If you're using jQuery 1.4's Ajax function with ASP.NET MVC, you can turn all DateTime properties into Date objects with:
In jQuery 1.5 you can avoid overriding the
parseJSON
method globally by using the converters option in the Ajax call.http://api.jquery.com/jQuery.ajax/
Unfortunately you have to switch to the older eval route in order to get Dates to parse globally in-place - otherwise you need to convert them on a more case-by-case basis post-parse.
Using the jQuery UI datepicker - really only makes sense if you're already including jQuery UI:
output:
Click here to check the Demo
JavaScript/jQuery
Result - "10/15/2008"
There is no built in date type in JSON. This looks like the number of seconds / milliseconds from some epoch. If you know the epoch you can create the date by adding on the right amount of time.
This may can also help you.
For those using Newtonsoft Json.NET, read up on how to do it via Native JSON in IE8, Firefox 3.5 plus Json.NET.
Also the documentation on changing the format of dates written by Json.NET is useful: Serializing Dates with Json.NET
For those that are too lazy, here are the quick steps. As JSON has a loose DateTime implementation, you need to use the
IsoDateTimeConverter()
. Note that since Json.NET 4.5 the default date format is ISO so the code below isn't needed.The JSON will come through as
Finally, some JavaScript to convert the ISO date to a JavaScript date:
I used it like this