How do I format a Microsoft JSON date?

后端 未结 30 3266
伪装坚强ぢ
伪装坚强ぢ 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

    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;
    }
    

提交回复
热议问题