我需要计算两个日期对象之间的经过时间 [英] I need to calculate an elapsed time between two date objects

查看:146
本文介绍了我需要计算两个日期对象之间的经过时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从保存在数据库中的变种创建的日期对象。

I have a date object created from vars saved in a database.

var prevTime = Date(year,month,day,hour,minute);

我想计算这与当前时间之间的差异。

I want to calculate the difference between this and the current time.

var thisTime = Date();

我这样做:

prevTime.getTime() - thisTime.getTime();

它给出了一个非常大的负数。我除以1000得到秒,然后除以3600得到几小时。我需要一个小时的经过时间。我最终得到一个像-756.00的数字。如果当前时间大于前一次,为什么数字为负数?我做错了什么?

It gives me a negative number that is very large. I divide by 1000 to get seconds and then divide by 3600 to get hours. I need an elapsed time in hours. I end up with a number that is like -756.00. If the current time is larger than the previous time, why is the number negative? What am I doing wrong?

谢谢,

Scott

推荐答案

当前时间大于上一次,因此从较小的数字中减去较大的数字会得到一个负数。如果您想要时间的正差异,请撤消订单。还有更多的问题吗?

The current time is larger than the previous time so subtracting a larger number from a smaller number gives you a negative number. Reverse the order if you want the positive difference in times. Is there more to the question than that?

这里示范: http://jsfiddle.net/jfriend00/NYSsp/

var prevTime = new Date(2011,1,1,0,0);  // Feb 1, 2011
var thisTime = new Date();              // now
var diff = thisTime.getTime() - prevTime.getTime();   // now - Feb 1
alert(diff / (1000*60*60*24));     // positive number of days

这篇关于我需要计算两个日期对象之间的经过时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆