js獲取當前時間,並將標準日期格式轉換成Long型
阿新 • • 發佈:2019-02-12
獲取當前日期:
//獲取當前時間
var nowDate = new Date();
var year= nowDate.getFullYear();
var month = nowDate.getMonth()+1;
var today = nowDate.getDate();
var hours = nowDate.getHours();
var minutes = nowDate.getMinutes();
var seconds = nowDate.getSeconds();
if(month >= 1 && month <=9){
month = "0" + month;
}
if(today >= 1 && today <=9){
today = "0" + today;
}
var currentdate = year + "-" + month + "-" + today + " " + hours + ":" +minutes + ":" +seconds;
將標準日期格式轉成Long型:
var currentDateLong = new Date(currentdate.replace(new RegExp("-","gm"),"/")).getTime() //當前時間轉換成long型
結果:
當前日期:
2017-05-10 12:52:56
轉換結果:
1494391976