How to calculate date difference in JavaScript?

前端 未结 18 1755
执笔经年
执笔经年 2020-11-22 03:41

I want to calculate date difference in days, hours, minutes, seconds, milliseconds, nanoseconds. How can I do it?

相关标签:
18条回答
  • 2020-11-22 04:09

    based on javascript runtime prototype implementation you can use simple arithmetic to subtract dates as in bellow

    var sep = new Date(2020, 07, 31, 23, 59, 59);
    var today = new Date();
    var diffD = Math.floor((sep - today) / (1000 * 60 * 60 * 24));
    console.log('Day Diff: '+diffD);
    

    the difference return answer as milliseconds, then you have to convert it by division:

    • by 1000 to convert to second
    • by 1000×60 convert to minute
    • by 1000×60×60 convert to hour
    • by 1000×60×60×24 convert to day
    0 讨论(0)
  • 2020-11-22 04:10
    var DateDiff = {
    
        inDays: function(d1, d2) {
            var t2 = d2.getTime();
            var t1 = d1.getTime();
    
            return parseInt((t2-t1)/(24*3600*1000));
        },
    
        inWeeks: function(d1, d2) {
            var t2 = d2.getTime();
            var t1 = d1.getTime();
    
            return parseInt((t2-t1)/(24*3600*1000*7));
        },
    
        inMonths: function(d1, d2) {
            var d1Y = d1.getFullYear();
            var d2Y = d2.getFullYear();
            var d1M = d1.getMonth();
            var d2M = d2.getMonth();
    
            return (d2M+12*d2Y)-(d1M+12*d1Y);
        },
    
        inYears: function(d1, d2) {
            return d2.getFullYear()-d1.getFullYear();
        }
    }
    
    var dString = "May, 20, 1984";
    
    var d1 = new Date(dString);
    var d2 = new Date();
    
    document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
    document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
    document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
    document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));
    

    Code sample taken from here.

    0 讨论(0)
  • 2020-11-22 04:11
    var d1=new Date(2011,0,1); // jan,1 2011
    var d2=new Date(); // now
    
    var diff=d2-d1,sign=diff<0?-1:1,milliseconds,seconds,minutes,hours,days;
    diff/=sign; // or diff=Math.abs(diff);
    diff=(diff-(milliseconds=diff%1000))/1000;
    diff=(diff-(seconds=diff%60))/60;
    diff=(diff-(minutes=diff%60))/60;
    days=(diff-(hours=diff%24))/24;
    
    console.info(sign===1?"Elapsed: ":"Remains: ",
                 days+" days, ",
                 hours+" hours, ",
                 minutes+" minutes, ",
                 seconds+" seconds, ",
                 milliseconds+" milliseconds.");
    
    0 讨论(0)
  • 2020-11-22 04:11

    I think this should do it.

    let today = new Date();
    let form_date=new Date('2019-10-23')
    let difference=form_date>today ? form_date-today : today-form_date
    let diff_days=Math.floor(difference/(1000*3600*24))
    
    0 讨论(0)
  • 2020-11-22 04:14
    function DateDiff(b, e)
    {
        let
            endYear = e.getFullYear(),
            endMonth = e.getMonth(),
            years = endYear - b.getFullYear(),
            months = endMonth - b.getMonth(),
            days = e.getDate() - b.getDate();
        if (months < 0)
        {
            years--;
            months += 12;
        }
        if (days < 0)
        {
            months--;
            days += new Date(endYear, endMonth, 0).getDate();
        }
        return [years, months, days];
    }
    
    [years, months, days] = DateDiff(
        new Date("October 21, 1980"),
        new Date("July 11, 2017")); // 36 8 20
    
    0 讨论(0)
  • 2020-11-22 04:14

    This is how you can implement difference between dates without a framework.

    function getDateDiff(dateOne, dateTwo) {
            if(dateOne.charAt(2)=='-' & dateTwo.charAt(2)=='-'){
                dateOne = new Date(formatDate(dateOne));
                dateTwo = new Date(formatDate(dateTwo));
            }
            else{
                dateOne = new Date(dateOne);
                dateTwo = new Date(dateTwo);            
            }
            let timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
            let diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
            let diffMonths = Math.ceil(diffDays/31);
            let diffYears = Math.ceil(diffMonths/12);
    
            let message = "Difference in Days: " + diffDays + " " +
                          "Difference in Months: " + diffMonths+ " " + 
                          "Difference in Years: " + diffYears;
            return message;
         }
    
        function formatDate(date) {
             return date.split('-').reverse().join('-');
        }
    
        console.log(getDateDiff("23-04-2017", "23-04-2018"));
    
    0 讨论(0)
提交回复
热议问题