(javascript或/和datejs)试图计算两个日期间隔总薪水。当开始日期和结束日期有一个月

问题描述:

****工资的每月的某一部分天。****(javascript或/和datejs)试图计算两个日期间隔总薪水。当开始日期和结束日期有一个月

var getDiffDatesSalary = function days_between(date1,date2, monthlyBasic) { 
     var dateObj1 = new Date(date1); 
     var month1 = dateObj1.getMonth(); //months from 1-12 
     var day1 = dateObj1.getDate(); 
     var year1 = dateObj1.getFullYear(); 
     var daysInMonth1 = Date.getDaysInMonth(year1, month1); 

     var dateObj2 = new Date(date2); 
     var month2 = dateObj2.getMonth(); //months from 0-11 
     var day2 = dateObj2.getDate();// days from 1 
     var year2 = dateObj2.getFullYear(); 
     var daysInMonth2 = Date.getDaysInMonth(year2, month2); 

     var date1FractionDays = daysInMonth1 - day1; 
     var date2FractionDays = daysInMonth2 - day2; 
     var newDate1, newDate2; 
     if(day1 > 1){ 
      var perDaySalary1 = monthlyBasic/daysInMonth1; 
      var thisMonthFarctionDaysSalaryForDate1 = perDaySalary1 * date1FractionDays; 
      month1 += 1; // after calculate fraction this month basic, round month from next 
      newDate1 = new Date(year1,month1); 
     } 
     if(day2 !== daysInMonth2){ 
      var perDaySalary2 = monthlyBasic/daysInMonth2; 
      var thisMonthFarctionDaysSalaryForDate2 = perDaySalary2 * day2; 
      month2 -= 1; //after calculate fraction this month basic, round month from previous 
      newDate2 = new Date(year2,month2); 
     } 

     // i want to calculate totalSalaryamount of date ranges 
     // var totalFractionDaysSalary = thisMonthFarctionDaysSalaryForDate1 + thisMonthFarctionDaysSalaryForDate2; 
     // var totalMonthsSalay = roundMonths * monthlyBasic; 
     // var totalSalaryamount = totalFractionDaysSalary + totalMonthsSalay; 

    }; 

**

结果= thisMonthFarctionDaysSalaryForDate1 + thisMonthFarctionDaysSalaryForDate2 +(roundMonths * monthlyBasic)

结果将是getDiffDatesSalary(“2016/01/15”,“2016/03/25”,1000);

516.13 + 806.45 + 1000 //总:2322.58

或/和getDiffDatesSalary( “2015/01/15”, “2016年3月25日”,1000);

516.13 + 806.45 +(1000 * 13)//总:14322.58

或/和getDiffDatesSalary( “2016年1月1日”, “2016/02/29”,1000);

1000 * 2 // 2000

**

+0

尝试使用['momentjs'(http://momentjs.com/) – Zamboney

+0

我需要帮助纠正这一功能。我已经尝试过,但不能..谢谢Zamboney –

+0

是有一天的同一天? –

我建议两个部分拆分任务:

  1. 获得总天数
  2. 计算工资。

本提案使用年,月,日之间的差异,如果日差为负,则进行修正。同一天意味着有一天。

function getDays(date1, date2) { 
 
    var date1Obj = new Date(date1), 
 
     date2Obj = new Date(date2), 
 
     totalYear = date2Obj.getFullYear() - date1Obj.getFullYear(), 
 
     totalMonth = date2Obj.getMonth() - date1Obj.getMonth(), 
 
     totalDay = date2Obj.getDate() - date1Obj.getDate() + 1; 
 

 
    if (totalDay < 0) { 
 
     totalMonth--; 
 
     totalDay += new Date(date1Obj.getFullYear(), date1Obj.getMonth(), 0).getDate(); 
 
    } 
 

 
    return 360 * totalYear + 30 * totalMonth + totalDay; 
 
} 
 

 
function getDiffDatesSalary(date1, date2, salaryPerMonth) { 
 
    return getDays(date1, date2) * salaryPerMonth/30; 
 
} 
 

 
document.write(getDiffDatesSalary("2016/01/15", "2016/03/25", 1000) + '<br>'); 
 
document.write(getDiffDatesSalary("2016/01/31", "2016/02/15", 1000) + '<br>'); 
 
document.write(getDiffDatesSalary("2016/03/20", "2016/03/20", 3000) + '<br>');

+0

谢谢你的一个好主意 –

+0

function getDiffDatesSalary(date1,date2,salaryPerMonth){ return getDays(date1,date2)* salaryPerMonth/30; } –

+0

它没有显示确切的结果..因为每个月都不是30天。如Feb 29。Jan 31,Apr 30 ....如果每月1000和date1是15/01,那么16天的工资将是(1000/31)* 16.如果date1是15/02,那么14天的工资将是1000/29)* 14。如12/02那样反向日期2,然后计算12天工资。 –

我得到了我的问题的答案。如果有人需要或改进这个答案。这里是工作代码:

var getDiffDatesSalary = function days_between(date1,date2, monthlyBasic) { 
    var dateObj1 = new Date(date1); 
    var month1 = dateObj1.getMonth(); //months from 0-11 
    var day1 = dateObj1.getDate(); 
    var year1 = dateObj1.getFullYear(); 
    var daysInMonth1 = Date.getDaysInMonth(year1, month1); 

    var dateObj2 = new Date(date2); 
    var month2 = dateObj2.getMonth(); //months from 0-11 
    var day2 = dateObj2.getDate();// days from 1 
    var year2 = dateObj2.getFullYear(); 
    var daysInMonth2 = Date.getDaysInMonth(year2, month2); 
    //get number of months in two different dates; 
    var diffMonths = parseInt(diffInMonths(date2,date1)) +1; //from 1-12 

    var date1FractionDays = daysInMonth1 - day1; // date1 fraction days 
    var date2FractionDays = daysInMonth2 - day2; // date2 fraction days 
    var totalFractionDaysSalary= 0, fractionMonthsCount = 0, thisMonthFarctionDaysSalaryForDate1 = 0, thisMonthFarctionDaysSalaryForDate2 =0; //reset as 0; 
    //when date1: day start from 01, no fraction start of the month. Otherwise calculate salary for fraction days 
    if(day1 > 1){ 
     var perDaySalary1 = monthlyBasic/daysInMonth1; 
     thisMonthFarctionDaysSalaryForDate1 = perDaySalary1 * date1FractionDays; 
     fractionMonthsCount +=1; 
    } 
    //when date2: day end === end Of the Month, no fraction. Otherwise calculate salary for fraction days 
    if(day2 !== daysInMonth2){ 
     var perDaySalary2 = monthlyBasic/daysInMonth2; 
     thisMonthFarctionDaysSalaryForDate2 = perDaySalary2 * day2; 
     fractionMonthsCount +=1; 
    } 
    // both date1 date2 fraction days salary sum 
    totalFractionDaysSalary = thisMonthFarctionDaysSalaryForDate1 + thisMonthFarctionDaysSalaryForDate2; 
    var roundMonthsForSalary = diffMonths - fractionMonthsCount; // after less round month calculate 
    // if user do wrong reset as 0. because negative month not possible 
    if (roundMonthsForSalary < 0){ 
     roundMonthsForSalary = 0; 
    } 
    // round month salary calculation 
    var totalSalaryForRoundMonths = roundMonthsForSalary * monthlyBasic; 
// finally fraction days and round month sum to get return result. 
return totalFractionDaysSalary + totalSalaryForRoundMonths; 

}; 
// get number of months in two different dates 
function diffInMonths(to,from){ 
    var months = to.getMonth() - from.getMonth() + (12 * (to.getFullYear() - from.getFullYear())); 

    if(to.getDate() < from.getDate()){ 
     var newFrom = new Date(to.getFullYear(),to.getMonth(),from.getDate()); 
     if (to < newFrom && to.getMonth() == newFrom.getMonth() && to.getYear() %4 != 0){ 
      months--; 
     } 
    } 

    return months; 
};