日期范围内的价格计算问题

时间:2012-10-26 11:39:56

标签: javascript

在收费系统上没有正确计算收费的问题。

下面是代码,如果您在一个日期范围内运行而不是以1个价格给出2天,然后说另一个价格的1天它不会。以下是代码示例:

$(function () {
    function change_charges() {
        var checkin_mon = Number($("#checkindate_Month_ID").val());
        var checkin_day = Number($("#checkindate_Day_ID").val());
        var checkin_year = Number($("#checkindate_Year_ID").val());
        var checkout_mon = Number($("#checkoutdate_Month_ID").val());
        var checkout_day = Number($("#checkoutdate_Day_ID").val());
        var checkout_year = Number($("#checkoutdate_Year_ID").val());
        var checkin_date = new Date();
        checkin_date.setFullYear(checkin_year, checkin_mon, checkin_day);
        var checkout_date = new Date();
        checkout_date.setFullYear(checkout_year, checkout_mon, checkout_day);
        var one_day = 1000 * 60 * 60 * 24
        //Calculate difference btw the two dates, and convert to days
        var stay = Math.ceil((checkout_date.getTime() - checkin_date.getTime()) / (one_day));
        var room_type = $("#room_type").val();
        var charges = 0;
        var n_charges = 0;
        var st_date = new Date();
        var end_date = new Date();
        var mon_stay = 0;
        var rem_stay = 0;
        if (room_type == "Villa") {
            st_date.setFullYear(checkin_year, 4, 1);
            end_date.setFullYear(checkin_year, 9, 31);
            if (checkin_date >= st_date && checkin_date <= end_date) {
                charges = 1995;
                n_charges = 2995;
                if (checkout_date > end_date) mon_stay = Math.ceil((end_date.getTime() - checkin_date.getTime()) / (one_day));
            }
            st_date.setFullYear(checkin_year, 10, 1);
            end_date.setFullYear(checkin_year, 11, 15);
            if (checkin_date >= st_date && checkin_date <= end_date) {
                charges = 2995;
                n_charges = 2995;
                if (checkout_date > end_date) mon_stay = Math.ceil((end_date.getTime() - checkin_date.getTime()) / (one_day));
            }
            st_date.setFullYear(checkin_year, 11, 16);
            end_date.setFullYear(checkin_year, 11, 31);
            if (checkin_date >= st_date && checkin_date <= end_date) {
                charges = 5900;
                n_charges = 5900;
                if (checkout_date > end_date) mon_stay = Math.ceil((end_date.getTime() - checkin_date.getTime()) / (one_day));
            }
            st_date.setFullYear(checkin_year, 0, 1);
            end_date.setFullYear(checkin_year, 0, 15);
            if (checkin_date >= st_date && checkin_date <= end_date) {
                charges = 5900;
                n_charges = 3900;
                if (checkout_date > end_date) mon_stay = Math.ceil((end_date.getTime() - checkin_date.getTime()) / (one_day));
            }
            st_date.setFullYear(2013, 0, 16);
            end_date.setFullYear(2013, 3, 30);
            if (checkin_date >= st_date && checkin_date <= end_date) {
                charges = 3900;
                n_charges = 2223;
                if (checkout_date > end_date) mon_stay = Math.ceil((end_date.getTime() - checkin_date.getTime()) / (one_day));
            }

if(stay&gt; 1){

   if(mon_stay == 0)mon_stay = 1;

   if(mon_stay != 0){
      rem_stay = stay - mon_stay;

      charges = (mon_stay*charges) + (rem_stay*n_charges);   

   }

   else
   charges*=stay;

}

var today = new Date();

if(checkin_date&lt; today || checkout_date&lt; today || checkout_date&lt; checkin_date){

    charges = 0;


}

$( “#tot_charges”)HTML(charges.toFixed(2));

}

$(“#checkindate_Month_ID”)。change(function(){

change_charges();

});

$(“#checkindate_Day_ID”)。change(function(){

change_charges();

});

$(“#checkindate_Year_ID”)。change(function(){

change_charges();

});

$(“#checkoutdate_Month_ID”)。change(function(){

change_charges();

});

$(“#checkoutdate_Day_ID”)。change(function(){

change_charges();

});

$(“#checkoutdate_Year_ID”)。change(function(){

change_charges();

});

$(“#room_type”)。change(function(){

change_charges();

});

1 个答案:

答案 0 :(得分:1)

你的计算功能错了: 以你的第一个例子为例:3天@ 1995 = 5985 您编码的是以下内容: 留= 3 mon_stay = 1(因为你写道:如果mon_stay = 0则mon_stay = 1) 那么

费用= 1 * 1995 + 2 * 2995 = 7985!

一个更好的公式(我认为但很难理解)

charges = (mon_stay*n_charges) + (rem_stay*charges);

没有mon_stay = 1(除非有意)