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

 

 

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