JSON後臺序列化日期類型變量(非字符串)在前端接收解析後的JS對象日期格式顯示異常

 後臺序列化JSON對象時,注意日期類型變量在序列化後將轉換成諸如格式:\"\\\/Date(1568131200000)\\\/\";前端接收到的變量格式:/Date(1568044800000)/;所以需要通過函數將其轉換爲正常日期格式顯示,如:2019-09-11 00:00:00。


            function TransDate(time) {
                if (time!= null) {
                    var date = new Date(parseInt(time.replace("/Date(", "").replace(")/", ""), 10)); 
                    //月份爲0-11,所以+1,月份小於10時補個0                    
                    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                    var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                    var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                    var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                    return date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minute + ":" + second;
                }
                return "";
            }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章