jquery - How do I format a Microsoft JSON date? -
i'm taking first crack @ ajax jquery. i'm getting data onto page, i'm having trouble json data returned date data types. basically, i'm getting string looks this:
/date(1224043200000)/
from totally new json - how format short date format? should handled somewhere in jquery code? i've tried jquery.ui.datepicker
plugin using $.datepicker.formatdate()
without success.
fyi: here's solution came using combination of 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 object callback method , displayed dates on page using date format library.
eval not necessary. work fine:
var date = new date(parseint(jsondate.substr(6)));
the substr function takes out "/date(" part, , parseint function gets integer , ignores ")/" @ end. resulting number passed date constructor.
edit: have intentionally left out radix (the 2nd argument parseint); see my comment below. also, agree rory's comment: iso-8601 dates preferred on old format -- format shouldn't used new development. see excellent json.net library great alternative serializes dates using iso-8601 format.
for iso-8601 formatted json dates, pass string date constructor:
var date = new date(jsondate); //no ugly parsing needed; full timezone support
Comments
Post a Comment