JS 時間字符串與時間戳之間的轉換
阿新 • • 發佈:2019-04-17
minutes 時間戳 .get ont nds time() timestamp mes rep
1、當前時間換時間戳
var timestamp = parseInt(new Date().getTime()/1000); // 當前時間戳 document.write(timestamp);
2、當前時間換日期字符串
var now = new Date(); var yy = now.getFullYear(); //年 var mm = now.getMonth() + 1; //月 var dd = now.getDate(); //日 var hh = now.getHours(); //時 var ii = now.getMinutes(); //分 var ss = now.getSeconds(); //秒 var clock = yy + "-"; if(mm < 10) clock += "0"; clock += mm + "-"; if(dd < 10) clock += "0"; clock += dd + " "; if(hh < 10) clock += "0"; clock += hh + ":"; if (ii < 10) clock += ‘0‘; clock += ii + ":"; if (ss < 10) clock += ‘0‘; clock += ss; document.write(clock);//獲取當前日期
3、日期字符串轉時間戳
var date = ‘2015-03-05 17:59:00.0‘; date = date.substring(0,19); date = date.replace(/-/g,‘/‘); var timestamp = new Date(date).getTime(); document.write(timestamp);
4、時間戳轉日期字符串
var timestamp = ‘1425553097‘; var d = new Date(timestamp * 1000); //根據時間戳生成的時間對象 var date = (d.getFullYear()) + "-" + (d.getMonth()+ 1) + "-" + (d.getDate()) + " " + (d.getHours()) + ":" + (d.getMinutes()) + ":" + (d.getSeconds()); document.write(date);
JS 時間字符串與時間戳之間的轉換