不少時候從後臺拿到的時間數據是:"/Date(1511424240000+0800)/" 這種格式的,須要手動將其轉換成咱們須要的格式函數
//轉換到日期:spa
function datatime(dateS) {
var d = eval('new ' + dateS.substr(1,dateS.length - 2));
var ar_date = [d.getFullYear(), d.getMonth() + 1, d.getDate()];
for (var i = 0; i < ar_date.length; i++) ar_date[i] = dFormat(ar_date[i]);
return ar_date.join('-');
function dFormat(i) { return i < 10 ? "0" + i.toString() : i; }
}orm
例:get
var myddDate = "/Date(1511424240000+0800)/";
console.log(data_string(myddDate));string
輸出:2017-11-23io
//轉換日期、時間函數console
function DateTime (dateS) {
var date = eval('new '+dateS.substr(1,dateS.length - 2));
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
var h = date.getHours();
var mi = date.getMinutes();
h = h < 10 ? ('0' + h) : h;
mi = mi < 10 ? ('0' + mi) : mi;
return y + '-' + m + '-' + d+' '+h+':'+mi;
};function
var myddDate = "/Date(1511424240000+0800)/";後臺
console.log(DateTime (myddDate));date
輸出:2017-11-23 16:04
這就是咱們所須要的數據。