Javascript 我需要计算两个日期对象之间经过的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6636621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I need to calculate an elapsed time between two date objects
提问by llihttocs
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();
I am doing this:
我正在这样做:
prevTime.getTime() - thisTime.getTime();
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?
它给了我一个非常大的负数。我除以 1000 得到秒,然后除以 3600 得到小时。我需要以小时为单位的经过时间。我最终得到一个类似于 -756.00 的数字。如果当前时间比前一时间大,为什么数字是负数?我究竟做错了什么?
Thanks,
谢谢,
Scott
斯科特
回答by jfriend00
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?
当前时间比前一个时间大,因此从较小的数字中减去较大的数字会得到一个负数。如果您想要时间上的正差异,请颠倒顺序。还有比这更多的问题吗?
Demonstration here: http://jsfiddle.net/jfriend00/NYSsp/
演示在这里: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
回答by Aliostad
First one must be smaller that you get negative. Your first date is in the past.
第一个必须更小,否则您会得到负数。你的第一次约会已经过去了。
Here is an example:
下面是一个例子:
var a = new Date(2011,7,5,2,1,1) - new Date(2011,7,5,1,1,1);
alert(a); // 3600000 which is millisecond for 1 hour. Divide by this to get hours.
回答by user3313148
IF you want Date and Time in Dynamic Run on web Page and also the Login time i.e the clock starts from the start of java function scipt and also gives how many hours,minutes and seconds you are online in the Application or a Particular web Page For this just call the javascript function in the html body load.
如果您想要在网页上动态运行的日期和时间以及登录时间,即时钟从 java 函数 scipt 的开始开始,并且还提供您在应用程序或特定网页中在线的小时数、分钟数和秒数这只是在 html 正文加载中调用 javascript 函数。
<script type="text/javascript">
var second;
var first = new Date;
function date_time() {
var id = 'Label1'
date = new Date;
second = date.getTime();
year = date.getFullYear();
month = date.getMonth();
months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
d = date.getDate();
day = date.getDay();
days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
h = date.getHours();
if (h < 10) {
h = "0" + h;
}
m = date.getMinutes();
if (m < 10) {
m = "0" + m;
//min = min+1;
}
s = date.getSeconds();
if (s < 10) {
s = "0" + s;
//sec = sec+1;
}
var timeDiff = second - first.getTime();
var hours = Math.floor(timeDiff / (1000 * 60 * 60));
timeDiff -= hours * (1000 * 60 * 60);
var mins = Math.floor(timeDiff / (1000 * 60));
timeDiff -= mins * (1000 * 60);
var secs = Math.floor(timeDiff / 1000)
timeDiff -= secs * 1000;
result = 'LoginTime(HH:MM:SS) ' + hours + ':' + mins + ':' + secs + ' ' + days[day] + ' ' + months[month] + ' ' + d + ' ' + year + ' Clock(24hr): ' + h + ':' + m + ':' + s;
document.getElementById(id).innerHTML = result;
setTimeout('date_time("' + id + '");', '1000');
return true;
}
By Santosh Kumar Murarkar
桑托什·库马尔·穆拉卡
回答by Saeed Neamati
Check this:
检查这个:
var first = new Date();
for(i = 0; i<10000; i++) // Some loop to make a little time pass
{
i = i;
}
var second = new Date();
second.getTime() - first.getTime();