Date difference in Javascript (ignoring time of day)

前端 未结 15 2209
无人共我
无人共我 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:10

    I know this is a partial solution but you may be able to get the days between dates (as long as the dates don't have time set). Then you can work from there.

    I'm basing this solution with the problem you presented ((daily fee * number of days) = total charge); not the actual date difference question. This should help you out.

    var Main = function() {
        var startDate = new Date('08/20/2014');
        var endDate = new Date('08/22/2014');
        var totalCharge = monthlyFee * daysBetween(startDate, endDate);
    }
    
    var daysBetween = function(startDate, endDate) {
        return Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(24*60*60*1000)));
    };
    
    0 讨论(0)
  • 2020-11-27 16:17

    If you only want the difference in days, be sure to use UTC (GMT) or the subtraction won't produce a whole number of days in seconds if Daylight Saving Time starts or end during the interval.

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

    use setHours() method, assuming number of days can never be zero :)

    function dateDiff1(startDate, endDate) {
        endDate.setHours(0);
        startDate.setHours(0);
        //assuming days cannt be 0.
    
        var x = ((endDate.getTime() - startDate.getTime()) / 1000*60*60*24);
        if (x <1 && x>=0) {
            return 1;
        }
        return x;
    }
    
    0 讨论(0)
  • 2020-11-27 16:17

    Just out of curiosity:

    If you can affirm that the outcome will ALWAYS be < 31 days, then the following is a solution too:

    var deltaDays = new Date(endDate - startDate).getDate()-1
    

    Why?

    Well decomposing the previous line:

    var msecs = endDate - startDate; // difference in milliseconds
    var dt = new Date(msecs); // will give us a date object pointing to a time 
                              // somewhere in Jan 1970 (given the precondition above)
    var days = dt.getDate();  // Take the day part of it
    var deltaDays = days - 1; // since the numbering starts from 1, we have to 
                              // substract 1 (Ex. if the diff in msecs is 0. The
                              // Date will point to 1st of Jan 1970)
    

    But obviously you should NOT use it if your results can be > 31.

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

    What I would do is set the two date's times to the same time. For example, set endDate's time to 12:00am and startDate's time to 12:00 am also. Then get the difference between them.

    On a side note, since I too am in the rental equipment software industry, it seems like you're losing rental revenue by not counting the hours. Per your example if someone picked up the equipment on July 6th at 6am and returned it on july 7th at 10pm. They had two full days to use the equipment and possibly incur an excess meter charge too...

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

    Need date.js, http://code.google.com/p/datejs/

    function CalculateDuration(startDate, endDate) {

    var sdt = Date.parse(startDate);
    var edt = Date.parse(endDate);
    
    var sdtYear = sdt.getYear();
    var edtYear = edt.getYear();
    var sdtMonth = sdt.getMonth();
    var edtMonth = edt.getMonth();
    var sdtDay = sdt.getDate();
    var edtDay = edt.getDate();
    
    var StartDateMonthDays = Date.getDaysInMonth(sdtYear, sdtMonth);
    var EndDateMonthDays = Date.getDaysInMonth(edtYear, edtMonth);
    
    if (edtMonth < sdtMonth) { //Means the ending month is earlier, which invalidates the operation
    
        alert("Ending month cannot be earlier than the starting month")
    
    }
    
    //Months are equal, if month is the next month of the start date month, 
    //the operation fails, thus we need to calculate month difference first
    else if (edtMonth > sdtMonth) {
    
        //Need to count how many days are left to finish the month
        var daysToMonthFinish = StartDateMonthDays - sdtDay;
        var calculatedDuration = daysToMonthFinish + edtDay;
        $("#hdn_duration").val(calculatedDuration);
    
    }
    //If months are the same, it is safe to just calculate the end day minus the start day
    else {
    
        var calcDuration = edtDay - sdtDay;
        $("#hdn_duration").val(calcDuration);
    }
    
    0 讨论(0)
提交回复
热议问题