在 JavaScript 中计算不包括周末和联邦假期的前一个工作日

我正在编写一个函数,它将为我提供任何给定日期的前一个工作日。工作日意味着它是工作日,并且当天没有联邦假期。

我的解决方案在今天(案例 1)是“2019-06-20T07:00:00.000Z”时有效;即星期四 // 返回星期三此外,当今天(情况 2)为“2019-06-24T07:00:00.000Z”时有效,即星期一 // 返回星期五

但是当今天(情况 3)是 2019-05-28T07:00:00.000Z // 星期二时失败。

对于情况 3,它应该返回前一个工作日为 5 月 24 日(星期五),因为星期一(5 月 27 日)是假期。对于案例 3,它返回 5 月 27 日,星期一。

下面是我的代码,在我的 or 语句中我正在检查前一天,但它没有考虑到这一点,


噜噜哒
浏览 195回答 2
2回答

青春有我

您没有充分发挥 Luxon 的潜力。您应该将日期保留为 Luxon 对象,使用 Luxon 的方法对其进行所有操作,然后将日期转换为字符串。为此,我定义了一个辅助函数prevBusinessDayHelper,该函数采用 Luxon 日期时间并返回表示前一个工作日的日期时间。它完全按照 Luxon 日期时间运行,这很容易。然后在外部函数中,我在 Luxon 日期时间之间进行转换。const DateTime = luxon.DateTime;// use a Set to make lookups cheaperconst federalHolidays = new Set([&nbsp; '2019-05-27', // <-- you were missing the 0 here in yours&nbsp; '2019-09-02',&nbsp; // snip]);// recursion is good here because it's very shallowconst prevBusinessDayHelper = dt => {&nbsp; // use luxon's tools!&nbsp; const yest = dt.minus({ days: 1 });&nbsp; if (yest.weekday == 6 || yest.weekday == 7 || federalHolidays.has(yest.toISODate()))&nbsp; &nbsp; return prevBusinessDayHelper(yest);&nbsp; return yest;};const prevBusinessDay = (isoString, zone) => {&nbsp; const dt = DateTime.fromISO(isoString).setZone(zone);&nbsp; return prevBusinessDayHelper(dt).toISODate();};console.log(prevBusinessDay("2019-05-28T07:00:00.000Z", "America/New_York"));<script src="https://cdn.jsdelivr.net/npm/luxon@1.21.1/build/global/luxon.min.js"></script>

慕斯709654

当您检查假期数组时,您正在检查完整日期而不是日期部分。function check_previous_business_date(date, timezone) {&nbsp; &nbsp; &nbsp; const startDate = new Date(luxon.DateTime.fromISO(date).setZone(timezone));&nbsp; &nbsp; &nbsp; const todayTimeStamp = +new Date(startDate); // Unix timestamp in milliseconds&nbsp; &nbsp; &nbsp; const oneDayTimeStamp = 1000 * 60 * 60 * 24; // Milliseconds in a day&nbsp; &nbsp; &nbsp; const diff = todayTimeStamp - oneDayTimeStamp;&nbsp; &nbsp; &nbsp; const yesterdayDate = new Date(diff);&nbsp; &nbsp; &nbsp; const yesterdayString = yesterdayDate.getFullYear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ '-' + (yesterdayDate.getMonth() + 1) + '-' + yesterdayDate.getDate();&nbsp; &nbsp; &nbsp; for (startDate.setDate(startDate.getDate() - 1);&nbsp; &nbsp; &nbsp; &nbsp; !startDate.getDay() || startDate.getDay() === 6 ||&nbsp; &nbsp; &nbsp; &nbsp; federalHolidays.includes(startDate.toISOString().split('T')[0]) ||&nbsp; &nbsp; &nbsp; &nbsp; federalHolidays.includes(yesterdayString);&nbsp; &nbsp; &nbsp; &nbsp; startDate.setDate(startDate.getDate() - 1)&nbsp; &nbsp; &nbsp; ) {&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return startDate.toISOString().split('T')[0];&nbsp; &nbsp; }const federalHolidays= [&nbsp; '2019-05-27',&nbsp; '2019-09-02',&nbsp; '2019-10-14',&nbsp; '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'));<script src="https://cdn.jsdelivr.net/npm/luxon@1.21.1/build/global/luxon.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript