Skip to content Skip to sidebar Skip to footer

Javascript Date And Json Datetime

I have a problem with Jquery function getJSON, the action url does not trigger because one of the parameter i am passing is a javascript date but the action expects c# DateTime.. I

Solution 1:

I would suggest using the Datejs library (http://www.datejs.com/). From my limited experience with it it's fantastic.

Solution 2:

Use this function taken from the Mozilla Date documentation:

/* use a function for the exact format desired... */functionISODateString(d){
 functionpad(n){return n<10 ? '0'+n : n}
 return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth()+1)+'-'
      + pad(d.getUTCDate())+'T'
      + pad(d.getUTCHours())+':'
      + pad(d.getUTCMinutes())+':'
      + pad(d.getUTCSeconds())+'Z'
}

.NET will have no problem handling an ISO formatted date. You can use DateTime.Parse(...) to handle the ISO formatted string.

Solution 3:

If you are trying for a solution to get a Javascript date from the JSON representation (/Date(1350035703817)/) you can use this function:

functionparseJsonDate(jsonDate) {
    var offset = newDate().getTimezoneOffset() * 60000;
    var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);

    if (parts[2] == undefined) 
      parts[2] = 0;

    if (parts[3] == undefined) 
      parts[3] = 0;

    returnnewDate(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
};

Worked for me like charm.

Solution 4:

I used this function, shorter than the above one.

functionParseJsonDate(dateString) {
    var milli = dateString.replace(/\/Date\((-?\d+)\)\//, '$1');
    var date = newDate(parseInt(milli));
    return date;
}

Also found a method to convert them back:

functionToJsonDate(date) {
    return'\/Date(' + date.getTime() + ')\/';
}

Post a Comment for "Javascript Date And Json Datetime"