Javascript - 日期范围之间的月份拆分金额

时间:2014-01-21 12:05:08

标签: javascript date

我希望根据每个月的天数在月份之间平均分配金额。它将有3个输入 - start_date,end_date和amount。我需要每个月分一次作为回报。

以下是三个例子:

金额= 300,000

A:
start date 9/1/2013 - end date 11/31/2013
September: 300,000 / (30+31+30) * 30
October: 300,000 / (30+31+30) * 31
November: 300,000 / (30+31+30) * 30

B:
start date 9/15/2013 - end date 11/30/2013
September: 300,000 / (15+31+30) * 15
October: 300,000 / (15+31+30) * 31
November: 300,000 / (15+31+30) * 30

C:
start date 9/15/2013 - end date 11/15/2013
September: (15+31+15) * 15
October: (15+31+15) * 30
November: (15+31+15) * 15

计算特定月份金额的数学将是:(总金额/总天数)*(本月包括的#天数)

2 个答案:

答案 0 :(得分:0)

尝试仅计算300附近的天数。

试试这个:

var dateDif = {
                dateDiff: function(strDate1,strDate2)
                {
                    return (((Date.parse(strDate2))-(Date.parse(strDate1)))/(24*60*60*1000)).toFixed(0);
                }
}

function diasDecorridos(dt1, dt2){
   var minuto = 60000; 
   var dia = minuto * 60 * 24;

   dt1.setHours(0);
   dt1.setMinutes(0);
   dt1.setSeconds(0);
   dt2.setHours(0);
   dt2.setMinutes(0);
   dt2.setSeconds(0);

   var fh1 = dt1.getTimezoneOffset();
   var fh2 = dt2.getTimezoneOffset(); 


   var dif = Math.abs(dt2.getTime() - dt1.getTime()) ;
   return Math.ceil(dif / dia);

答案 1 :(得分:0)

我想出了这个。它完成了这项工作。有什么改进的建议吗?

getMonthsBetweenDates : function(s,e) {
    return e.getMonth() - s.getMonth() + (12 * (e.getFullYear() - s.getFullYear())) + 1;
}

diffDays: function(start_date, end_date) {  
    var stop_date = new Date(end_date);
    stop_date.setHours(23);
    stop_date.setMinutes(59);
    stop_date.setSeconds(59);
    return Math.ceil(Math.abs((start_date - stop_date.getTime())/(24*60*60*1000)));
}


var diff_days = util.diffDays(start_date, end_date);
    num_months = util.getMonthsBetweenDates(new Date(start_date), new Date(end_date)),
    splitup_data = new Array(),
    total_budget = data.budget;

var budget_per_day = total_budget / diff_days;

if(num_months > 0) {

    var allotted_budget = 0;

    for(var i=0; i<num_months; i++) {

        start_year_month = new Date(start_date).getFullYear() + '-' + new Date(start_date).getMonth();
        end_year_month = new Date(end_date).getFullYear() + '-' + new Date(end_date).getMonth();                

        temp_date = new Date(start_date);
        temp_date.setMonth(temp_date.getMonth() + i);                       
        tmp_year_month = temp_date.getFullYear() + '-' + temp_date.getMonth();      

        if(tmp_year_month == start_year_month) {
            month_start_date = new Date(start_date); 
            month_end_date = new Date(start_date);                  
            month_end_date.setMonth(month_end_date.getMonth()+1);
            month_end_date.setDate(0);

            month_days = util.diffDays(month_start_date.getTime(), month_end_date.getTime());
            month_budget = Math.round(budget_per_day * month_days);
            allotted_budget+= month_budget;

        } else if(tmp_year_month == end_year_month) {
            month_budget = total_budget - allotted_budget;
        } else {            

            month_start_date = new Date(temp_date.getTime());
            month_end_date = new Date(temp_date.getTime());
            month_start_date.setDate(1);                    
            month_end_date.setMonth(month_end_date.getMonth()+1);
            month_end_date.setDate(0);

            month_days = util.diffDays(month_start_date.getTime(), month_end_date.getTime());
            month_budget = Math.round(budget_per_day * month_days);
            allotted_budget+= month_budget;
        }

        splitup_data.push({            
            "month": temp_date.toYMD(),
            "contracted": month_budget
        });
    }
}       

return splitup_data;