javascript - 以分钟为单位比较两个时间戳 JS

标签 javascript time

嗨,我正在使用这个时间戳:

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/

相关文章:

iOS - backgroundTimeRemaining 值未显示预期值

php - 发布当前评论时间

javascript - 将 linq 查询转换为 linqjs

javascript - 如何在分派(dispatch)到 reducer 之前从操作中调用函数?

javascript - addEventListener 在 firefox 中工作但在 chrome 中不工作

停止表单提交的 JavaScript 代码

ios - 如何在 iOS 上获取 "internet time"?

javascript - 更新 JavaScript

Asp.net 时间输入控制建议

c# - 在 C# 中使用复选框启用和禁用 TextBox