(javascript or/and datejs)试图计算两个日期间隔的总工资。当开始日期和结束日期有一个月的小数天
(javascript or/and datejs)Trying to calculate two dates intervals total salary. when start date and end date has some fraction days of month
****月薪类型。****
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
Or/And getDiffDatesSalary("2015/01/15","2016/03/25", 1000);
516.13+806.45 + (1000 * 13) // 总计:14322.58
Or/And getDiffDatesSalary("2016/01/01","2016/02/29", 1000);
1000* 2 // 2000
**
我得到了问题的答案。如果有人需要或改进这个答案。这是工作代码:
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;
};
我建议将任务分成两部分:
- 获取总天数
- 计算工资。
本方案采用年月日之差,日差为负时进行修正。同一天就是一天。
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>');
****月薪类型。****
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
Or/And getDiffDatesSalary("2015/01/15","2016/03/25", 1000);
516.13+806.45 + (1000 * 13) // 总计:14322.58
Or/And getDiffDatesSalary("2016/01/01","2016/02/29", 1000);
1000* 2 // 2000
**
我得到了问题的答案。如果有人需要或改进这个答案。这是工作代码:
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;
};
我建议将任务分成两部分:
- 获取总天数
- 计算工资。
本方案采用年月日之差,日差为负时进行修正。同一天就是一天。
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>');