javascript:计算两个日期之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14919201/
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
javascript: Calculate difference between two dates
提问by mcacorner
I want to find difference between two Dates. For that I did subtract one Date object from another Date object. My code is as follows :
我想找到两个日期之间的差异。为此,我确实从另一个 Date 对象中减去了一个 Date 对象。我的代码如下:
var d1 = new Date(); //"now"
var d2 = new Date(2012,3,17); // before one year
document.write("</br>Currrent date : "+d1);
document.write("</br>Other Date : "+d2);
document.write("</br>Difference : "+new Date(Math.abs(d1-d2)));
But the result is not as I expected:
但结果并不如我预期:
Currrent date : Sun Feb 17 2013 02:58:16 GMT-0500 (EST)
Other Date : Sat Jan 21 2012 00:00:00 GMT-0500 (EST)
Difference : Thu Jan 28 1971 21:58:16 GMT-0500 (EST)
当前日期:2013 年 2 月 17 日星期日 02:58:16 GMT-0500 (EST)
其他日期:2012 年 1 月 21 日星期六 00:00:00 GMT-0500 (EST)
差异:1971 年 1 月 28 日星期四 21:58:16 GMT-05 (美东时间)
I want to calculate the (1 year) difference between them.
我想计算它们之间的(1 年)差异。
Thanks
谢谢
回答by James
If you need to be fairly accurate I suggest using days as your unit. Years have a variable number of days so do months, so saying "1 month" or "1 year" can mean different #s of days.
如果您需要相当准确,我建议您使用天作为单位。年的天数是可变的,月份也是如此,所以说“1 个月”或“1 年”可能意味着不同的天数。
var d1 = new Date(); //"now"
var d2 = new Date(2012,3,17); // before one year
var msPerDay = 1000*60*60*24;
document.write( ((d1 - d2) / msPerDay).toFixed(0) + " days ago");
回答by migg
So fundamentally the biggest exact date unit is a weekwhich accounts for 7 * 86400 seconds. Months and Years are not exaclty defined. So assuming you want to say "1 Month ago" if the two dates are e.g. 5.1.2013
and 5.2.2013
or 5.2.2013
and 5.3.2013
. And saying "1 Month and 1 day ago" if you have e.g. 5.1.2013
and 6.2.2013
, then you would have to use a calculation like this:
所以基本上最大的精确日期单位是一周,占 7 * 86400 秒。月和年没有精确定义。所以假设你想说“1 Month ago”,如果两个日期是 eg 5.1.2013
and 5.2.2013
or 5.2.2013
and 5.3.2013
。并说“1 个月零 1 天前”如果您有 eg5.1.2013
和6.2.2013
,那么您将不得不使用这样的计算:
// dateFrom and dateTo have to be "Date" instances, and to has to be later/bigger than from.
function dateDiff(dateFrom, dateTo) {
var from = {
d: dateFrom.getDate(),
m: dateFrom.getMonth() + 1,
y: dateFrom.getFullYear()
};
var to = {
d: dateTo.getDate(),
m: dateTo.getMonth() + 1,
y: dateTo.getFullYear()
};
var daysFebruary = to.y % 4 != 0 || (to.y % 100 == 0 && to.y % 400 != 0)? 28 : 29;
var daysInMonths = [0, 31, daysFebruary, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (to.d < from.d) {
to.d += daysInMonths[parseInt(to.m)];
from.m += 1;
}
if (to.m < from.m) {
to.m += 12;
from.y += 1;
}
return {
days: to.d - from.d,
months: to.m - from.m,
years: to.y - from.y
};
}
// Difference from 1 June 2016 to now
console.log(dateDiff(new Date(2016,5,1), new Date()));
As I said, it gets tricky ;)
正如我所说,它变得棘手;)
回答by Exception
回答by user3519216
Are you looking for this?
你在找这个吗?
Math.ceil((new Date(2012, 11, 23) - new Date(2012, 11, 21)) / 864000) + 1
Math.ceil((new Date(2012, 11, 23) - new Date(2012, 11, 21)) / 864000) + 1