【问题标题】:Get difference between two dates using jquery使用jquery获取两个日期之间的差异
【发布时间】:2011-05-01 07:39:10
【问题描述】:

谁能推荐一个 jQuery 插件来计算两个日期之间的差异(日期也可能包含时间)并将其显示为“32 天”、“13 小时”、“20 分钟”等?

【问题讨论】:

标签: jquery jquery-plugins


【解决方案1】:

您可以使用 native JavaScript Date 对象进行添加、减去和执行许多其他操作。 powerful enough 可以满足您的大部分需求。如果使用它,您可以节省数千字节的页面大小并使代码对每个人都易于理解(可能 90% 的 JavaScript 开发人员从未使用任何插件来计算日期)

我讨厌 Date 对象的一点是它没有built-in formatted output

例如,如果不进行字符串解析,就无法知道本地化的星期几或月份名称。然后datejs 来帮你。

Date 对象能做什么?

var msMinute = 60*1000, 
    msDay = 60*60*24*1000,
    a = new Date(2012, 2, 12, /* days, hours*/ 23, 59, 59),
    b = new Date("2013 march 12"), /* string */
    c = new Date(),                /* now */
    d = new Date(c.getTime() + msDay - msMinute);       /* tomorrow - minute */


console.log(a.getUTCHours());
console.log(typeof (b - a + 1000));
console.log(Math.floor((b - a) / msDay) + ' full days between');
console.log(Math.floor(((b - a) % msDay) / msMinute) + ' full minutes between');
console.log('Today is ' + c.getDay() + ' day of week');
console.log('Tomorrow is ' + d.getDay() + ' day of week');
console.log('Your timezone offset is ' + c.getTimezoneOffset() + ' minutes');

Easily calculate days till Christmas

而且,有时一个笑话中的真相比你想象的要多

【讨论】:

  • 如果添加一个数字到日期,请始终使用getTime()。此代码不起作用console.log(a + 10) 而这有效console.log(a.getTime() + 10)
【解决方案2】:

这是我刚刚编写的一个非常简单的 Javascript 实现。您可以通过数学计算将其延长到几个月或几年,或者根据需要删除 1 值的复数形式。

var dateDiff = function ( d1, d2 ) {
    var diff = Math.abs(d1 - d2);
    if (Math.floor(diff/86400000)) {
        return Math.floor(diff/86400000) + " days";
    } else if (Math.floor(diff/3600000)) {
        return Math.floor(diff/3600000) + " hours";
    } else if (Math.floor(diff/60000)) {
        return Math.floor(diff/60000) + " minutes";
    } else {
        return "< 1 minute";
    }
};

dateDiff(new Date(1990, 1, 1), new Date(1990, 1, 13)) // -> 12 days

【讨论】:

    【解决方案3】:

    我强烈建议您使用出色的 datejs framework 轻松完成所有日期时间计算

    【讨论】:

    • date.js 从未通过 Alpha-1 并且自 2007 年以来已经过时。我非常不鼓励使用它!
    【解决方案4】:

    我认为jQuery EasyDate 正是您想要的。

    【讨论】:

    • 谢谢,确认这对我有用,并且确实会实现所要求的。提供了大量示例。
    • 2018 年链接失效
    • 谢谢,我更新了 Archive.org 快照。
    【解决方案5】:
    day1= $("#tMTLCLsDay").val();
    month1= $("#tMTLCLsMnth").val();
    year1= $("#tMTLCLsYr").val();
    hour1= $("#tMTLCLs_HOUR").val();
    min1= $("#tMTLCLs_MIN").val();
    var Date1=month1+ "/" + day1+ "/" + year1+ " " + hour1 + ":" + min1+ ": 00" ;
    
    day2= $("#tMTLCLsDay").val();
    month2= $("#tMTLCLsMnth").val();
    year2= $("#tMTLCLsYr").val();
    hour3= $("#tMTLCLs_HOUR").val();
    hour2= $("#tMTLCLs_MIN").val();
    var Date2=month2+ "/" + day2+ "/" + year2+ " " + hour2 + ":" + hour2 + ": 00" ;
    
    
    if(Date.parse(Date1)<Date.parse(Date2))
    {
    alert("Date 2 is large");
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2017-03-27
      • 1970-01-01
      • 2010-12-18
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多