一、当前时间转换为标准格式:
function getFormatDate ( ) {
var date = new Date ( ) ;
var month = date. getMonth ( ) + 1 ;
var day = date. getDate ( ) ;
var hour = date. getHours ( ) ;
var minutes = date. getMinutes ( ) ;
var seconds = date. getSeconds ( ) ;
month = ( month< 10 ) ? '0' + month : month;
day = ( day< 10 ) ? '0' + day : day;
hour = ( hour< 10 ) ? '0' + hour : hour;
minutes = ( minutes< 10 ) ? '0' + minutes : minutes;
seconds = ( seconds< 10 ) ? '0' + seconds : seconds;
var currentDate = date. getFullYear ( ) + "-" + month + "-" + day
+ " " + hour + ":" + minutes + ":" + seconds;
return currentDate;
}
console. log ( getFormatDate ( ) )
二、时间戳转换为标准格式:
function formatDate ( datetime) {
var date = new Date ( datetime) ;
var month = ( "0" + ( date. getMonth ( ) + 1 ) ) . slice ( - 2 ) ,
sdate = ( "0" + date. getDate ( ) ) . slice ( - 2 ) ,
hour = ( "0" + date. getHours ( ) ) . slice ( - 2 ) ,
minute = ( "0" + date. getMinutes ( ) ) . slice ( - 2 ) ,
second = ( "0" + date. getSeconds ( ) ) . slice ( - 2 ) ;
var thatDate = date. getFullYear ( ) + "-" + month + "-" + sdate + " " + hour + ":" + minute + ":" + second;
return thatDate;
}
console. log ( formatDate ( 1599085447000 ) )