I am writing a function which will provide me previous working day for any given date. Working day means it is a weekday and there\'s no federal holiday on that day.
My
When you are checking for holidays array you are checking full date instead of the date part.
function check_previous_business_date(date, timezone) {
const startDate = new Date(luxon.DateTime.fromISO(date).setZone(timezone));
const todayTimeStamp = +new Date(startDate); // Unix timestamp in milliseconds
const oneDayTimeStamp = 1000 * 60 * 60 * 24; // Milliseconds in a day
const diff = todayTimeStamp - oneDayTimeStamp;
const yesterdayDate = new Date(diff);
const yesterdayString = yesterdayDate.getFullYear()
+ '-' + (yesterdayDate.getMonth() + 1) + '-' + yesterdayDate.getDate();
for (startDate.setDate(startDate.getDate() - 1);
!startDate.getDay() || startDate.getDay() === 6 ||
federalHolidays.includes(startDate.toISOString().split('T')[0]) ||
federalHolidays.includes(yesterdayString);
startDate.setDate(startDate.getDate() - 1)
) {
}
return startDate.toISOString().split('T')[0];
}
const federalHolidays= [
'2019-05-27',
'2019-09-02',
'2019-10-14',
'2019-11-11'
];
console.log('Prev. day of 2019-05-28 is ',check_previous_business_date('2019-05-28T07:00:00.000Z', 'America/New_York'));
console.log('Prev. day of 2019-06-20 is ',check_previous_business_date('2019-06-20T07:00:00.000Z', 'America/New_York'));