I know somebody already answered this question, but this might be helpful to you or others in the future. This is adapted from a tutorial on Twilio and runs as one of their serverless functions.
There is a JSON object containing holidays, office hours, and partial days. The function returns an object with a handful of flags (open, holiday, etc.), the greeting (morning, afternoon, evening), and the reason if it's a holiday or partial day.
const Moment = require( 'moment-timezone' );
const MomentRange = require( 'moment-range' );
const moment = MomentRange.extendMoment( Moment );
const timezone = "America/New_York";
exports.handler = async function(context, event, callback) {
let response = new Twilio.Response();
let client = context.getTwilioClient();
response.setStatusCode( 200 );
response.appendHeader('Access-Control-Allow-Origin', '*');
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
response.appendHeader('Content-Type', 'application/json');
let body = getCalendarInfo();
response.setBody(body);
callback(null, response);
};
function checkIfInRange( begin, end, timezone ) {
const currentDate = moment().tz( timezone ).format( 'MM/DD/YYYY' );
const now = moment().tz( timezone );
const beginMomentObject = moment.tz( `${currentDate} ${begin}`, 'MM/DD/YYYY HH:mm:ss', timezone );
const endMomentObject = moment.tz( `${currentDate} ${end}`, 'MM/DD/YYYY HH:mm:ss', timezone );
const range = moment.range( beginMomentObject, endMomentObject );
return now.within( range );
}
function getCalendarInfo() {
let body = {
isOpen: false,
isHoliday: false,
isPartialDay: false,
isRegularDay: false,
greeting: '',
reason: '',
date: '',
time: ''
};
//load JSON with schedule
const schedule = {
"holidays": {
"01/03/2023": {
"reason": "the New Year Holiday"
},
"01/16/2023": {
"reason": "Martin Luther King Jr Day"
}
},
"partialDays": {
// "12/26/2019": {
// "begin": "10:00:00",
// "end": "14:00:00",
// "reason": "the Day after Christmas"
// }
},
"regularHours": {
"Monday": {
"begin": "08:00:00",
"end": "18:00:00"
},
"Tuesday": {
"begin": "08:00:00",
"end": "18:00:00"
},
"Wednesday": {
"begin": "08:00:00",
"end": "18:00:00"
},
"Thursday": {
"begin": "08:00:00",
"end": "18:00:00"
},
"Friday": {
"begin": "08:00:00",
"end": "23:59:00"
},
"Saturday": {
"begin": null,
"end": null
},
"Sunday": {
"begin": null,
"end": null
}
}
}
body.date = moment().tz( timezone ).format( 'MM/DD/YYYY' );
body.time = moment().tz( timezone ).format( 'HH:mm:ss' );
if ( body.time < "12:00:00" ) {
body.greeting = "morning";
} else if ( body.time < "18:00:00" ) {
body.greeting = "afternoon";
} else {
body.greeting = "evening";
}
const currentDate = moment().tz( timezone ).format( 'MM/DD/YYYY' );
const isHoliday = currentDate in schedule.holidays;
const isPartialDay = currentDate in schedule.partialDays;
if ( isHoliday ) {
body.isHoliday = true;
if ( typeof( schedule.holidays[ currentDate ].reason ) !== 'undefined' ) {
body.reason = schedule.holidays[ currentDate ].reason;
}
} else if ( isPartialDay ) {
body.isPartialDay = true;
if ( typeof( schedule.partialDays[ currentDate ].reason ) !== 'undefined' ) {
body.reason = schedule.partialDays[ currentDate ].reason;
}
if ( checkIfInRange( schedule.partialDays[ currentDate ].begin,
schedule.partialDays[ currentDate ].end, timezone ) === true ) {
body.isOpen = true;
}
} else {
//regular hours
const dayOfWeek = moment().tz( timezone ).format( 'dddd' );
body.isRegularDay = true;
if ( checkIfInRange( schedule.regularHours[ dayOfWeek ].begin,
schedule.regularHours[ dayOfWeek ].end, timezone ) === true ) {
body.isOpen = true;
}
}
return body;
}