找出两个日期之间的区别 [英] Find the difference between two dates
本文介绍了找出两个日期之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以这种格式找到两个日期之间的差异
年份,月份(如果年份为++ 12,则必须为<12>,天数(必须为30,如果> 30,那么月份++),小时(必须<24,如果> 24天天++)
How can i find the difference between two dates in this format Year, Month (Must be < 12 if > 12 the Year++), Days ( Must be < 30 if > 30 then month++), Hours (must be < 24 if > 24 then day++)
我不会有这样的格式
3,
月34(我会算这个34年),
dasy 345(一个这个值)
year 3, month 34 (i will calc this 34 in years), dasy 345 ( an this value too)
我有这个代码
var currentTo = new Date(2015, 6, 1),
currentFrom = new Date(2013,11,1),
year = currentTo.getFullYear() - currentFrom.getFullYear(),
m1 = currentTo.getMonth() + 1,
m2 = currentFrom.getMonth() + 1,
month = m1 <= m2 ? (12 - m2) + m1 : m1 - m2;
alert("From: " + currentFrom);
alert("To :" + currentTo);
if (currentTo.getDate() < currentFrom.getDate()) {
month = month - 1;
}
if (month >= 12){ month = 0; }
alert(year + ' ' + month);
但我不知道我可以计算几天的时间
but i have no idea how i can calc days an hours
推荐答案
不要单独减少年,月,日等等,只是得到日期之间的差异(以毫秒为单位),然后以格式(或单位)输出)你想要的:
Don't substract year, month, day etc separately, but just get the difference between the dates (in milliseconds) and then output that in a format (or unit) that you want:
var currentTo = new Date(2015, 6, 1),
currentFrom = new Date(2013,11,1),
difference = currentTo - currentFrom; // number conversion is implicit
var hours = difference / 3600000; // ms -> h
// now do your maths
这篇关于找出两个日期之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文