当后台给返回的时间是时间戳的时候如何进行转换成想要的格式

//格式时间
var len = 21315489756454;
var date = new Date(len);
var Y = date.getFullYear() + '年';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
var D = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate() + '日';
console.log(Y+M+D);
// 比如需要这样的格式 yyyy-MM-dd hh:mm:ss
var date = new Date(1398250549490);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds(); 
console.log(Y+M+D+h+m+s); 
// 输出结果:2014-04-23 18:55:49

猜你喜欢

转载自blog.csdn.net/fengxiaopeng74/article/details/81563770
今日推荐