折腾:
后,需要把js中Date转换为时间戳
js date to timestamp
convert date to timestamp in javascript? – Stack Overflow
javascript – Convert normal date to unix timestamp – Stack Overflow
convert iso date to milliseconds in javascript – Stack Overflow
Date.parse() – JavaScript | MDN
【总结】
最后用代码:
// 对Date的扩展,返回时间戳(毫秒,13位) // 例子: // this=Tue Jun 06 2017 15:00:00 GMT+0800 (CST) -> timesamp=1496732400000 Date.prototype.timestamp = function () { // console.log(`Date timesamp: this=${this}`); let timesamp = this.getTime(); // console.log(`this=${this} -> timesamp=${timesamp}`); return timesamp; }; |
(注:当然其实也可以用Date.getTime()直接获得时间戳,但是效果不够好,意思不明显,所以还是封装后的效果好)
调用:
let preFiveHoursTimestamp = preFiveHoursDate.timestamp(); let nexSevenHoursTimestamp = nexSevenHoursDate.timestamp(); console.log(`preFiveHoursTimestamp=${preFiveHoursTimestamp},nexSevenHoursTimestamp=${nexSevenHoursTimestamp}`); |
效果:
Date timesamp: this=Tue Jun 06 2017 15:00:00 GMT+0800 (CST) VM12371:78 this=Tue Jun 06 2017 15:00:00 GMT+0800 (CST) -> timesamp=1496732400000 index.js?7def:472 preFiveHoursTimestamp=1496689200000,nexSevenHoursTimestamp=1496732400000 |
转载请注明:在路上 » 【已解决】js中日期转为时间戳