1

I'm using jquery datepicker to calculate price according to selected date. Now it's set 125 for one month. I want to make a discount 5% if customer chooses 3 months period and 10% if customers chooses 4+ months.

How to do it?

http://jsfiddle.net/5BbGS/502/

    function showDays() {
    var start = $('#arr_date').datepicker('getDate');
    var end = $('#dep_date').datepicker('getDate');
    if (!start || !end) return;
    var days = (end - start) / 1000 / 60 / 60 / 24;
    var dayss = days*4.166666666666667;
    dayss = dayss.toFixed(0);
    $('#num_nights').val(dayss);
}

$("#arr_date").datepicker({
    dateFormat: 'dd-mm-yy',
    onSelect: showDays,
     onClose: function( selectedDate ) {
         var dParts = selectedDate.split('-');
         var in30Days = new Date(dParts[2] + '/' +
                        dParts[1] + '/' +
                        (+dParts[0] + 30)
               );

    $( "#dep_date" ).datepicker( "option", "minDate", in30Days );
    }
});
$("#dep_date").datepicker({
    dateFormat: 'dd-mm-yy',
    onSelect: showDays,

});

Thank you guys!

Xinel
  • 119
  • 13

1 Answers1

1

Use this function...

function showDays() {
    var start = $('#arr_date').datepicker('getDate');
    var end = $('#dep_date').datepicker('getDate');
    if (!start || !end) return;
    var days = (end - start) / 1000 / 60 / 60 / 24;
    var dayss = days*4.166666666666667;
    dayss = dayss.toFixed(0);
    if(days>90 && days<=120) dayss = dayss*95/100;
    if(days>120) dayss = dayss*90/100;
    $('#num_nights').val(dayss);
}

http://jsfiddle.net/5BbGS/507/

But i guess 3 months doesn't means 90 days, you need to look into it...

Here is a function to get the number of months between two dates:

function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() ;
    months += d2.getMonth();
    return months <= 0 ? 0 : months;
}

http://jsfiddle.net/5BbGS/514/

void
  • 36,090
  • 8
  • 62
  • 107