Date difference in Javascript (ignoring time of day)

前端 未结 15 2207
无人共我
无人共我 2020-11-27 15:35

I\'m writing an equipment rental application where clients are charged a fee for renting equipment based on the duration (in days) of the rental. So, basically, (daily fee *

相关标签:
15条回答
  • 2020-11-27 16:01

    I would also suggest having a look at the incredible moment.js library. It has a versatile diff function, which you can use to solve the above example as follows:

    function is_same_date(startDate, endDate) {
       var startMoment = moment(startDate).clone().startOf('day'),
           endMoment = moment(endDate).clone().startOf('day');
       return startMoment.diff(endMoment, 'days') == 0;
    }
    

    Here are some examples using moment.js diff:

    > d1 = new Date(2012,04,04,0,0)
    Fri May 04 2012 00:00:00 GMT-0400 (EDT)
    
    > d2 = new Date(2012,04,03,23,0)
    Thu May 03 2012 23:00:00 GMT-0400 (EDT)
    
    > d3 = new Date(2012,04,05,23,0)
    Sat May 05 2012 23:00:00 GMT-0400 (EDT)
    
    > moment(d2).diff(d1, 'days')
    0
    
    > moment(d1).diff(d2, 'days')
    0
    
    > moment(d1).diff(d3, 'days') // uh oh. this is why we use `startOf`
    -2
    
    > moment(d2).diff(d3, 'days')
    -2
    
    > moment(d3).startOf('day') // this modified d3, sets the time to 00:00:00.0000
    > d3
    Sat May 05 2012 00:00:00 GMT-0400 (EDT)
    

    If timezones are a concern, you can also use moment.utc() to convert to a normalized timezone.

    0 讨论(0)
  • 2020-11-27 16:02

    I used to check the result of datediff function which uses .getTime() with Excel for the 18th century. The result returns correct. I have another method to calculate different days:

        function DaysOfYear(nYear) {
            if ((nYear % 4) == 0) {
                return 366;
            }
            else {
                return 365;
            }
        }
    
    
        function DiffDays(fDate, tDate) {
            var fYear = fDate.getFullYear();
            var tYear = tDate.getFullYear();
            var fMonth = fDate.getMonth();
            var tMonth = tDate.getMonth();
            var fDay = fDate.getDate();
            var tDay = tDate.getDate();
            var nDays = 0;
    
            if (tYear > fYear) {   // different year
                // remain days of from year
                nDays = DaysOfYear(fYear) - YTD_Days(fDate);
                // days of between fYear to tYear
                for (y = (fYear + 1); y < tYear; y++) {
                    nDays = nDays + DaysOfYear(y);
                }
                // days from the beginning of tYear
                nDays = nDays + YTD_Days(tDate);
            }
            else {   // in the same year
                nDays = YTD_Days(tDate) - YTD_Days(fDate);
            };
            return nDays;
        }
    
        function YTD_Days(dDate) {
            var y = dDate.getFullYear();
            var m = dDate.getMonth();
            var nDays = 0
    
            for (i = 0; i < m; i++) {
                switch (i) {
                    case 0:     // January
                        nDays = nDays + 31;
                        break;
                    case 1:     // February
                        if ((y % 4) == 0) {
                            nDays = nDays + 29;
                        }
                        else {
                            nDays = nDays + 28;
                        };
                        break;
                    case 2:     // March
                        nDays = nDays + 31;
                        break;
                    case 3:     // April
                        nDays = nDays + 30;
                        break;
                    case 4:     // May
                        nDays = nDays + 31;
                        break;
                    case 5:     // June
                        nDays = nDays + 30;
                        break;
                    case 6:     // July
                        nDays = nDays + 31;
                        break;
                    case 7:     // August
                        nDays = nDays + 31;
                        break;
                    case 8:     // September
                        nDays = nDays + 30;
                        break;
                    case 9:     // October
                        nDays = nDays + 31;
                        break;
                    case 10:     // November
                        nDays = nDays + 30;
                        break;
                    case 11:     // December
                        nDays = nDays + 31;
                        break;
                }
            }
            nDays = nDays + dDate.getDate();
            return nDays;
        }
    
    0 讨论(0)
  • 2020-11-27 16:05

    If you want whole days for your student camera rental example ...

    function daysBetween(first, second) {
    
        // Copy date parts of the timestamps, discarding the time parts.
        var one = new Date(first.getFullYear(), first.getMonth(), first.getDate());
        var two = new Date(second.getFullYear(), second.getMonth(), second.getDate());
    
        // Do the math.
        var millisecondsPerDay = 1000 * 60 * 60 * 24;
        var millisBetween = two.getTime() - one.getTime();
        var days = millisBetween / millisecondsPerDay;
    
        // Round down.
        return Math.floor(days);
    }
    
    0 讨论(0)
  • 2020-11-27 16:07

    To get the full days the date interval over daylight saving time changes the floor(days) must be replaced by round. Otherwise it's a very useful function.

    0 讨论(0)
  • 2020-11-27 16:09

    I just had this problem and solved it after finding this question, so I came back to post this. This will get the total days regardless of time. And DST doesn't mess it up:

    date1 = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate());
    date2 = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate());
    var ms = Math.abs(date1-date2);
    return Math.floor(ms/1000/60/60/24); //floor should be unnecessary, but just in case
    

    The trick is converting to a UTC date that doesn't even know about the times of the original dates.

    0 讨论(0)
  • 2020-11-27 16:10

    Since the Julian day is effectively the number of days (and in some cases also the fraction of number of days) since a certain date, it is practically the same as a UNIX timestamp, presented differently. You can get the number of whole days since 1970 like so:

    Date.prototype.getWholeDays = function () {
        return Math.floor(new Date() / 1000*60*60*24);
    };
    
    function dateDiff(startDate, endDate) {
        return endDate.getWholeDays() - startDate.getWholeDays();
    }
    
    0 讨论(0)
提交回复
热议问题