嗨,我正在使用这个时间戳:
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
+ ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
.getSeconds()) : (now.getSeconds())));
}
并在此处节省暂停时间:
localStorage["TmpPause"] = getTimeStamp();
比过了一段时间阅读时间并比较:
var pauseTime = localStorage["TmpPause"];
var resumeTime = getTimeStamp();
var difference = resumeTime.getTime() - pauseTime.getTime(); // This will give difference in milliseconds
var resultInMinute = Math.round(difference / 60000);
alert(resultInMinute);
目前我无法计算包括时间在内的两个日期之间的差异。我收到错误 undefined is not a function resumeTime.getTime() ???
谢谢你的帮助。
最佳答案
getTimeStamp()
不返回时间戳(具有讽刺意味),而是返回以下格式的字符串:"1/20/2014 19:18:28"
这是您获得时间戳的方式(以毫秒为单位):Date.now()
, 甚至 Date().toString()
(以文本表示的时间)。
您应该存储它,以便以后可以通过执行以下操作重复使用它:
new Date(yourTimestamp); //this returns the original Date object
(newTimestamp - oldTimestamp)/1000/60 //returns difference in minute
关于javascript - 以分钟为单位比较两个时间戳 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21247357/