How do I format a Microsoft JSON date?

后端 未结 30 3191
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 04:48

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

相关标签:
30条回答
  • 2020-11-21 05:04

    Everyone of these answers has one thing in common: they all store dates as a single value (usually a string).

    Another option is to take advantage of the inherent structure of JSON, and represent a date as list of numbers:

    { "name":"Nick",
      "birthdate":[1968,6,9] }
    

    Of course, you would have to make sure both ends of the conversation agree on the format (year, month, day), and which fields are meant to be dates,... but it has the advantage of completely avoiding the issue of date-to-string conversion. It's all numbers -- no strings at all. Also, using the order: year, month, day also allows proper sorting by date.

    Just thinking outside the box here -- a JSON date doesn't have to be stored as a string.

    Another bonus to doing it this way is that you can easily (and efficiently) select all records for a given year or month by leveraging the way CouchDB handles queries on array values.

    0 讨论(0)
  • 2020-11-21 05:04

    Below is a pretty simple solution for parsing JSON dates. Use the below functions as per your requirement. You just need to pass the JSON format Date fetched as a parameter to the functions below:

    function JSONDate(dateStr) {
        var m, day;
        jsonDate = dateStr;
        var d = new Date(parseInt(jsonDate.substr(6)));
        m = d.getMonth() + 1;
        if (m < 10)
            m = '0' + m
        if (d.getDate() < 10)
            day = '0' + d.getDate()
        else
            day = d.getDate();
        return (m + '/' + day + '/' + d.getFullYear())
    }
    
    function JSONDateWithTime(dateStr) {
        jsonDate = dateStr;
        var d = new Date(parseInt(jsonDate.substr(6)));
        var m, day;
        m = d.getMonth() + 1;
        if (m < 10)
            m = '0' + m
        if (d.getDate() < 10)
            day = '0' + d.getDate()
        else
            day = d.getDate();
        var formattedDate = m + "/" + day + "/" + d.getFullYear();
        var hours = (d.getHours() < 10) ? "0" + d.getHours() : d.getHours();
        var minutes = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes();
        var formattedTime = hours + ":" + minutes + ":" + d.getSeconds();
        formattedDate = formattedDate + " " + formattedTime;
        return formattedDate;
    }
    
    0 讨论(0)
  • 2020-11-21 05:04

    This is frustrating. My solution was to parse out the "/ and /" from the value generated by ASP.NET's JavaScriptSerializer so that, though JSON may not have a date literal, it still gets interpreted by the browser as a date, which is what all I really want:{"myDate":Date(123456789)}

    Custom JavaScriptConverter for DateTime?

    I must emphasize the accuracy of Roy Tinker's comment. This is not legal JSON. It's a dirty, dirty hack on the server to remove the issue before it becomes a problem for JavaScript. It will choke a JSON parser. I used it for getting off the ground, but I do not use this any more. However, I still feel the best answer lies with changing how the server formats the date, for example, ISO as mentioned elsewhere.

    0 讨论(0)
  • 2020-11-21 05:06

    Posting in awesome thread:

    var d = new Date(parseInt('/Date(1224043200000)/'.slice(6, -2)));
    alert('' + (1 + d.getMonth()) + '/' + d.getDate() + '/' + d.getFullYear().toString().slice(-2));
    
    0 讨论(0)
  • 2020-11-21 05:06

    FYI, for anyone using Python on the server side: datetime.datetime().ctime() returns a string that is natively parsable by "new Date()". That is, if you create a new datetime.datetime instance (such as with datetime.datetime.now), the string can be included in the JSON string, and then that string can be passed as the first argument to the Date constructor. I haven't yet found any exceptions, but I haven't tested it too rigorously, either.

    0 讨论(0)
  • 2020-11-21 05:07

    Using the jQuery UI datepicker - really only makes sense if you're already including jQuery UI:

    $.datepicker.formatDate('MM d, yy', new Date(parseInt('/Date(1224043200000)/'.substr(6)))); 
    

    output:

    October 15, 2008

    0 讨论(0)
提交回复
热议问题