js時間Date格式轉換
阿新 • • 發佈:2020-11-26
獲取現在時間 並轉換格式
class Point{ nowTime(format){ //時間轉換格式 let date = new Date(),times; if(format=='yy-mm-dd hh:mm:ss'){//yy-mm-dd hh:mm:ss times=date.getFullYear() +"-"+ ((date.getMonth()+1)>=10?(date.getMonth()+1):'0'+ (date.getMonth()+1)) +"-"+ (date.getDate()>=10? date.getDate():'0'+ date.getDate())+" "+ (date.getHours()>=10? date.getHours():'0'+ date.getHours())+":"+ (date.getMinutes()>=10? date.getMinutes():'0'+ date.getMinutes())+":"+ (date.getSeconds()>=10? date.getSeconds():'0'+ date.getSeconds()); }else if(format=='yy-mm-dd'){//yy-mm-dd times = date.toLocaleDateString().replace(/\//g,'\-'); }else if(format=='hh:mm:ss'){ times = (date.getHours()>=10? date.getHours():'0'+ date.getHours())+":"+ (date.getMinutes()>=10? date.getMinutes():'0'+ date.getMinutes())+":"+ (date.getSeconds()>=10? date.getSeconds():'0'+ date.getSeconds()); }else{ times = date.toLocaleString().replace(/\//g, "\-"); } return times; } }; let time = new Point(); console.log(time.nowTime('yy-mm-dd hh:mm:ss'))