找出两个日期之间的区别

编程入门 行业动态 更新时间:2024-10-25 04:25:57
本文介绍了找出两个日期之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何以这种格式找到两个日期之间的差异年份,月份(如果年份为++ 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

更多推荐

找出两个日期之间的区别

本文发布于:2023-10-18 23:52:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   两个   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!