JavaScript中的年份,月份,日期的两个日期之间的差异 [英] Difference between two dates in years, months, days in JavaScript
问题描述
有很多解决方案,但它们只提供了几个月或几个月或几年的格式差异,或者它们不正确照顾一个月的实际天数或闰年等)。这真的很难做到吗?
我已经看过:
- http://momentjs.com/ - >只能输出任何年份,月份或日期的差异
- http://www.javascriptkit.com/javatutors/datedifference.shtml
- http://www.javascriptkit.com/jsref/date .shtml
- http://timeago.yarp.com/
- www.stackoverflow.com - >搜索功能
在php中容易,但不幸的是,我只能在该项目中使用客户端脚本。任何图书馆或框架都可以做到这一点也不错。
以下是日期差异的预期输出列表:
//预期输出应为:1年,5个月。
diffDate(new Date('2014-05-10'),new Date('2015-10-10'));
//预期产出应为:1年,4个月,29天。
diffDate(new Date('2014-05-10'),new Date('2015-10-09'));
//预期产出应为:1年,3个月,30天。
diffDate(new Date('2014-05-10'),new Date('2015-09-09'));
//预期产出应为:9个月,27天。
diffDate(new Date('2014-05-10'),new Date('2015-03-09'));
//预期产出应为:1年9个月28天。
diffDate(new Date('2014-05-10'),new Date('2016-03-09'));
//预期产出应为:1年,10个月,1天。
diffDate(new Date('2014-05-10'),new Date('chalk3-11'));
你需要多么精准?如果您确实需要考虑常见的年份和闰年,以及几个月之间的确切差异,那么您必须编写更先进的内容,但是基本和粗略的计算应该是诀窍:
today = new Date()
past = new Date(2010,05,01)//记住这相当于06 01 2010
//在js中的日期从0开始,所以05是june
函数calcDate(date1,date2){
var diff = Math.floor(date1.getTime - date2.getTime());
var day = 1000 * 60 * 60 * 24;
var days = Math.floor(diff / day);
var months = Math.floor(days / 31);
var years = Math.floor(months / 12);
var message = date2.toDateString();
message + =was
message + = days +days
message + = months +months
message + =年+年前\\\
返回消息
}
a = calcDate(今天过去)
console.log(a)//返回Tue Jun 01 2010年是1143天36个月3年前
请记住,这是不精确的,为了计算完全精确的日期必须有一个日历,并知道一年是否是闰年,也是我计算月数的方式只是近似值。
但是您可以轻松改进。
I've been searching for 4 hours now, and have not found a solution to get the difference between two dates in years, months, and days in JavaScript, like: 10th of April 2010 was 3 years, x month and y days ago.
There are lots of solutions, but they only offer the difference in the format of either days OR months OR years, or they are not correct (meaning not taking care of actual number of days in a month or leap years, etc). Is it really that difficult to do that?
I've had a look at:
- http://momentjs.com/ -> can only output the difference in either years, months, OR days
- http://www.javascriptkit.com/javatutors/datedifference.shtml
- http://www.javascriptkit.com/jsref/date.shtml
- http://timeago.yarp.com/
- www.stackoverflow.com -> Search function
In php it is easy, but unfortunately I can only use client-side script on that project. Any library or framework that can do it would be fine, too.
Here are a list of expected outputs for date differences:
//Expected output should be: "1 year, 5 months".
diffDate(new Date('2014-05-10'), new Date('2015-10-10'));
//Expected output should be: "1 year, 4 months, 29 days".
diffDate(new Date('2014-05-10'), new Date('2015-10-09'));
//Expected output should be: "1 year, 3 months, 30 days".
diffDate(new Date('2014-05-10'), new Date('2015-09-09'));
//Expected output should be: "9 months, 27 days".
diffDate(new Date('2014-05-10'), new Date('2015-03-09'));
//Expected output should be: "1 year, 9 months, 28 days".
diffDate(new Date('2014-05-10'), new Date('2016-03-09'));
//Expected output should be: "1 year, 10 months, 1 days".
diffDate(new Date('2014-05-10'), new Date('2016-03-11'));
How precise do you need to be? If you do need to take into account common years and leap years, and the exact difference in days between months then you'll have to write something more advanced but for a basic and rough calculation this should do the trick:
today = new Date()
past = new Date(2010,05,01) // remember this is equivalent to 06 01 2010
//dates in js are counted from 0, so 05 is june
function calcDate(date1,date2) {
var diff = Math.floor(date1.getTime() - date2.getTime());
var day = 1000 * 60 * 60 * 24;
var days = Math.floor(diff/day);
var months = Math.floor(days/31);
var years = Math.floor(months/12);
var message = date2.toDateString();
message += " was "
message += days + " days "
message += months + " months "
message += years + " years ago \n"
return message
}
a = calcDate(today,past)
console.log(a) // returns Tue Jun 01 2010 was 1143 days 36 months 3 years ago
Keep in mind that this is imprecise, in order to calculate the date with full precision one would have to have a calendar and know if a year is a leap year or not, also the way I'm calculating the number of months is only approximate.
But you can improve it easily.
这篇关于JavaScript中的年份,月份,日期的两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!