JavaScript 中以年、月、日为单位的两个日期之间的差异 [英] Difference between two dates in years, months, days in JavaScript

查看:33
本文介绍了JavaScript 中以年、月、日为单位的两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了 4 个小时,但没有找到解决方案来获取 JavaScript 中两个日期之间的年、月和日差异,例如:2010 年 4 月 10 日是 3 年 x 月和 y几天前.

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?

我看过:

  • 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

在 php 中很容易,但不幸的是我只能在该项目中使用客户端脚本.任何可以做到这一点的库或框架也可以.

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 
"

    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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆