Javascript - 计算两个工作日之间的天数
Javascript - Calculate number of days between two weekdays
我正在寻找一个计算两个工作日之间天差的函数。像,例如。
周一 - 周五 = 4
周日 - 周六 = 6
谢谢!
只需创建一个包含工作日值的对象。
暂且称它为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'));
我正在寻找一个计算两个工作日之间天差的函数。像,例如。
周一 - 周五 = 4
周日 - 周六 = 6
谢谢!
只需创建一个包含工作日值的对象。
暂且称它为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'));