JS時間文字列とタイムスタンプの切り替え

3942 ワード

javascript  タイムスタンプは24時間で変換されます. 
var timeTrans = new Date(parseInt(1459408942) * 1000);
console.log(timeTrans.toLocaleString('chinese',{hour12:false}));
ラベル:
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);