Skip to content Skip to sidebar Skip to footer

Calculate Previous Working Day Excluding Weekends And Federal Holidays In Javascript

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 solution wor

Solution 1:

You are not using Luxon to its full potential. You should leave the date as a Luxon object, do all the operations on it using Luxon's methods, and then convert the date into a string.

To do that, I defined a helper function prevBusinessDayHelper that takes a Luxon datetime and returns a date time representing the previous business day. It operates entirely in terms of Luxon datetimes, which makes it easy. Then in the outer function, I convert to and from Luxon datetimes.

constDateTime = luxon.DateTime;

// use a Set to make lookups cheaperconst federalHolidays = newSet([
  '2019-05-27', // <-- you were missing the 0 here in yours'2019-09-02',
  // snip
]);

// recursion is good here because it's very shallowconstprevBusinessDayHelper = dt => {

  // use luxon's tools!const yest = dt.minus({ days: 1 });

  if (yest.weekday == 6 || yest.weekday == 7 || federalHolidays.has(yest.toISODate()))
    returnprevBusinessDayHelper(yest);

  return yest;
};

constprevBusinessDay = (isoString, zone) => {
  const dt = DateTime.fromISO(isoString).setZone(zone);
  returnprevBusinessDayHelper(dt).toISODate();
};

console.log(prevBusinessDay("2019-05-28T07:00:00.000Z", "America/New_York"));
<scriptsrc="https://cdn.jsdelivr.net/npm/luxon@1.21.1/build/global/luxon.min.js"></script>

Solution 2:

When you are checking for holidays array you are checking full date instead of the date part.

functioncheck_previous_business_date(date, timezone) {
      const startDate = newDate(luxon.DateTime.fromISO(date).setZone(timezone));
      const todayTimeStamp = +newDate(startDate); // Unix timestamp in millisecondsconst oneDayTimeStamp = 1000 * 60 * 60 * 24; // Milliseconds in a dayconst diff = todayTimeStamp - oneDayTimeStamp;
      const yesterdayDate = newDate(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'));
<scriptsrc="https://cdn.jsdelivr.net/npm/luxon@1.21.1/build/global/luxon.min.js"></script>

Post a Comment for "Calculate Previous Working Day Excluding Weekends And Federal Holidays In Javascript"