我有一个呼叫中心,我使用这个JS代码让消息在营业时间和营业时间/周末之后进来,它会显示一条消息,说是在支持时间之外,我的问题是,我如何让这个代码在节假日也能工作?
exports.handler = async function(context, event, callback) {
const moment = require('moment-timezone');
var now = moment().tz('America/New_York');
console.log('Current time-->'+now);
console.log('current hours-->'+now.hour());
let isWorkingHours = false;
//let isWorkingHours = true; // testing
const weekday = now.isoWeekday();
console.log('weekday-->'+weekday);
if (now.hour() >= 9 && now.hour() <= 17 && weekday <= 5) {
//if (now.hour() >= 4 && now.hour() <= 20 && weekday <= 2) { // testing
isWorkingHours = true;
//Console.log('This is outside working hours');
}
callback(null, {
isWorkingHours: isWorkingHours
});
}发布于 2021-08-26 18:39:23
我在StackOverflow上从别人的帖子中得到了这个假日检查器(我应该记录这个链接,但我忘了)。您可以遍历您感兴趣的所有日期,以创建完整的假日列表,或者只检查给定的日期是否为假日
function check_holiday (dt_date) {
// check simple dates (month/date - no leading zeroes)
var n_date = dt_date.getDate(),
n_month = dt_date.getMonth() + 1;
var s_date1 = n_month + '/' + n_date;
if ( s_date1 == '1/1' // New Year's Day
|| s_date1 == '6/14' // Flag Day
|| s_date1 == '7/4' // Independence Day
|| s_date1 == '11/11' // Veterans Day
|| s_date1 == '12/25' // Christmas Day
) return true;
// weekday from beginning of the month (month/num/day)
var n_wday = dt_date.getDay(),
n_wnum = Math.floor((n_date - 1) / 7) + 1;
var s_date2 = n_month + '/' + n_wnum + '/' + n_wday;
if ( s_date2 == '1/3/1' // Birthday of Martin Luther King, third Monday in January
|| s_date2 == '2/3/1' // Washington's Birthday, third Monday in February
|| s_date2 == '5/3/6' // Armed Forces Day, third Saturday in May
|| s_date2 == '9/1/1' // Labor Day, first Monday in September
|| s_date2 == '10/2/1' // Columbus Day, second Monday in October
|| s_date2 == '11/4/4' // Thanksgiving Day, fourth Thursday in November
) return true;
// weekday number from end of the month (month/num/day)
var dt_temp = new Date (dt_date);
dt_temp.setDate(1);
dt_temp.setMonth(dt_temp.getMonth() + 1);
dt_temp.setDate(dt_temp.getDate() - 1);
n_wnum = Math.floor((dt_temp.getDate() - n_date - 1) / 7) + 1;
var s_date3 = n_month + '/' + n_wnum + '/' + n_wday;
if ( s_date3 == '5/1/1' // Memorial Day, last Monday in May
) return true;
// misc complex dates
if (s_date1 == '1/20' && (((dt_date.getFullYear() - 1937) % 4) == 0)
// Inauguration Day, January 20th every four years, starting in 1937.
) return true;
if (n_month == 11 && n_date >= 2 && n_date < 9 && n_wday == 2
// Election Day, Tuesday on or after November 2.
) return true;
return false;
}发布于 2021-08-26 18:40:49
检查特定时间是否在我们的工作范围之外的逻辑在您的if语句中。
我会根据节假日或休息日的时间范围,将其抽象到某种存储中。您可以有一个表或缓存来存储这些假日(您应该提前加载它们)。
这样,您可以执行以下操作:
要检查日期是否匹配,您可以使用下面的命令(无论当前时间如何,当您在日期级别进行比较时,它将返回true ):
console.log(moment('2021-05-10 14:00:00').isSame('2021-05-10', 'day'));发布于 2021-08-26 19:11:23
以下是你可能想要检查的东西列表:
复活节“
/* Function that checks if office is available at a specific date
* @returns {boolean}
*/
function is_office_available (date = new Date()) {
// weekends, or actually any other day of the week
let day = date.getDay();
if ( [0 /*sunday*/, 6 /*saturday*/].includes(day) ) return false;
// "static date" holidays (formatted as 'DD/MM')
let dd_mm = date.getDate() + '/' + (date.getMonth()+1);
if ( ['24/12', '25/12'].includes(dd_mm) ) return false;
// TODO: "variable date" holidays
// specific times
// this logic has to be extended a LOT if, for example, you have
// the office open from 8:30 to 17:30 with a launch break from 12:00 to 13:00 on weekdays
// and from 9:00 to 12:00 only on mondays
let hh = date.getHours();
if (9 < hh || hh > 17) return false;
// if no test returned true then the office is going to be available on that date
return true;
}您可以简单地在if语句中调用此函数。
“可变日期”假期有一个相当长的(和无聊的)实现,这取决于您在国家办事处的假期。
https://stackoverflow.com/questions/68943673
复制相似问题