1

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..

Is it possible to format the Javascript Date to make it compatible for c# DateTime?

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

Use this function taken from the Mozilla Date documentation:

/* use a function for the exact format desired... */
function ISODateString(d){
 function pad(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.

Comments

2

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

function parseJsonDate(jsonDate) {
    var offset = new Date().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;

    return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
};

Worked for me like charm.

Comments

1

I used this function, shorter than the above one.

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

Also found a method to convert them back:

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

1 Comment

This doesn't seem to care about timezone information that is sometimes appended to JSON dates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.