首先是获取当前系统时间转换为时间戳
var timestamp = Date.parse(new Date());//获取当前时间 timestamp = timestamp / 1000;
然后是时间戳转yyyy-mm-dd
function fmtDate(inputTime){ var date = new Date(inputTime); var y = 1900+date.getYear(); var m = "0"+(date.getMonth()+1); var d = "0"+date.getDate(); return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length); } fmtDate(date)
最后是然后是时间戳转yyyy-MM-dd HH-mm-ss
function formatDateTime(inputTime) { var date = new Date(inputTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; }
formatDateTime(inputTime);