js格式化时间戳
/*
* 格式化时间戳
*
*/
let date = {
judge: function(m){
return m<10?'0'+m:m;
},
timeStampformatYMD: function(shijianchuo){
/*
* shijianchuo 时间戳ß
* 转换年月日
*/
if(!shijianchuo){
return '--'
}
var shijianchuo = parseInt(shijianchuo)
var time = new Date(shijianchuo);
var yy = time.getFullYear();
var m = time.getMonth()+1;
var dd = time.getDate();
var hh = time.getHours();
var mm = time.getMinutes();
var ss = time.getSeconds();
return yy+'-'+ date.judge(m)+'-'+date.judge(dd);
},
timeStampformatYMDHMS: function(shijianchuo){
/*
* shijianchuo 时间戳ß
* 转换年月日时分秒
*/
if(!shijianchuo){
return '--'
}
var shijianchuo = parseInt(shijianchuo)
var time = new Date(shijianchuo);
var yy = time.getFullYear();
var m = time.getMonth()+1;
var dd = time.getDate();
var hh = time.getHours();
var mm = time.getMinutes();
var ss = time.getSeconds();
return yy+'-'+ date.judge(m)+'-'+date.judge(dd)+' '+date.judge(hh)+':'+date.judge(mm)+':'+date.judge(ss);
},
newDateformatYMD: function(newDate){
if(!newDate){
return '--'
}
var time = new Date(newDate.getTime());
var yy = time.getFullYear();
var m = time.getMonth()+1;
var dd = time.getDate();
var hh = time.getHours();
var mm = time.getMinutes();
var ss = time.getSeconds();
return yy+'-'+ date.judge(m)+'-'+date.judge(dd)
},
newDateformatYMDHMS: function(newDate){
if(!newDate){
return '--'
}
var time = newDate;
var yy = time.getFullYear();
var m = time.getMonth()+1;
var dd = time.getDate();
var hh = time.getHours();
var mm = time.getMinutes();
var ss = time.getSeconds();
return yy+'-'+ date.judge(m)+'-'+date.judge(dd)+' '+date.judge(hh)+':'+date.judge(mm)+':'+date.judge(ss);
}
}
console.log(date.timeStampformatYMD(1552439828000))
console.log(date.timeStampformatYMDHMS(1552439828000))
console.log(date.newDateformatYMD(new Date()))
console.log(date.newDateformatYMDHMS(new Date()))