如何以这种格式找到两个日期之间的差异年份,月份(如果年份为++ 12,则必须为,天数(必须为30,如果> 30,那么月份++),小时(必须 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)
我有这个代码
jsfiddle/AQSWu/
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更多推荐
找出两个日期之间的区别
发布评论