Javascript - 计算两个工作日之间的天数

时间:2018-01-03 07:54:41

标签: javascript angularjs momentjs angular-moment

我正在寻找一个计算两个工作日之间日差的函数。就像,例如。

星期一 - 星期五= 4 周日 - 周六= 6

谢谢!

1 个答案:

答案 0 :(得分:2)

只需在工作日创建一个对象'值。

我们称之为weekdays。此对象的目的是保持一周内每天的值。

然后创建一个算法以找出差异



var weekdays = {
       "Monday" : 1,
       "Tuesday" : 2,
       "Wednesday" : 3,
       "Thursday" : 4,
       "Friday" : 5,
       "Saturday" : 6,
       "Sunday" : 7
}

getBetweenWeekDays = function(day1, day2){
   if(weekdays[day1] <= weekdays[day2])
     return weekdays[day2] - weekdays[day1];
   return 7 - weekdays[day1] + weekdays[day2];
};
console.log('Monday - Friday = ' + getBetweenWeekDays('Monday','Friday')); 
console.log('Sunday - Saturday = ' + getBetweenWeekDays('Sunday','Saturday'));
console.log('Wednesday - Tuesday = ' + getBetweenWeekDays('Wednesday','Tuesday'));
console.log('Tuesday - Wednesday = ' + getBetweenWeekDays('Tuesday','Wednesday'));
&#13;
&#13;
&#13;