我怎么可以转换以下日期的纪元:
1293683278
到以下可读日期
2010-06-23 9时57分58秒
使用Javascript?
谢谢!
我怎么可以转换以下日期的纪元:
1293683278
到以下可读日期
2010-06-23 9时57分58秒
使用Javascript?
谢谢!
var timestamp = 1293683278;
var date = new Date(timestamp*1000);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
alert(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
更多信息请参见JS日期文档
其他方式:
var timestamp = 1293683278;
var date = new Date(timestamp*1000);
var iso = date.toISOString().match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/)
alert(iso[1] + ' ' + iso[2]);
您可以使用moment.js这一点。
moment.unix(1293683278).format('YYYY-MM-DD HH:mm:ss');
码:
var timestamp = 1293683278;
var d = new Date(timestamp*1000);
alert(
d.getFullYear() + "-" +
("00" + (d.getMonth() + 1)).slice(-2) + "-" +
("00" + d.getDate()).slice(-2) + " " +
("00" + d.getHours()).slice(-2) + ":" +
("00" + d.getMinutes()).slice(-2) + ":" +
("00" + d.getSeconds()).slice(-2)
);
格式: YYYY-MM-DD HH:II:SS
结果: 2010-12-30 07:57:58
演示: https://jsfiddle.net/NabiKAZ/9ycovfxt/
注意:对于当前的时间,只需使用: var d = new Date();
代替。
/* Get timestamp */ var timestamp = 1293683278; document.querySelector("#timestamp").innerHTML = "Timestamp: " + timestamp; /* Get milisecond from timestamp */ var milisecond = 1293683278 * 1000; document.querySelector("#milisecond").innerHTML = "Milisecond: " + milisecond; /* Separate milisecond */ var datetime = new Date(milisecond); var year = datetime.getFullYear(); var month = datetime.getMonth() + 1; // (0-11) var date = datetime.getDate(); var hour = datetime.getHours(); var minute = datetime.getMinutes(); var second = datetime.getSeconds(); document.querySelector("#datetime").innerHTML = "Datetime: " + year + "-" + addZero(month) + "-" + addZero(date) + " " + addZero(hour) + ":" + addZero(minute) + ":" + addZero(second); // Add 0 if argument < 10 function addZero(i) { if (i < 10) { i = "0" + i }; return i; }
<p id="timestamp"><p> <p id="milisecond"><p> <p id="datetime"><p>