Get difference between two dates using jquery

后端 未结 5 2199
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 20:59

Can any one suggest a jQuery plugin for calculating the difference between two dates (dates may contain time also) and show it as \'32 days\', \'13 hours\', \'20 min\' etc?<

5条回答
  •  盖世英雄少女心
    2021-01-06 21:36

    You can add, subtract and do many other things with the native JavaScript Date object. It's powerful enough for most of your needs. If using it, you save kilobytes of page size and make the code understandable for everyone (probably 90% of JavaScript developers have never used any plugins to calculate dates)

    The thing I hate about Date object is it does not have a built-in formatted output.

    For example, you cannot tell the localized day of the week or month name without string parsing. Then datejs comes to help you.

    What can a Date object do?

    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

    And, sometimes there is more truth in a joke then you could ever expect OMG, a scary dream

提交回复
热议问题