Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: interval.includes #299

Open
MichaelLeeHobbs opened this issue Dec 1, 2022 · 0 comments
Open

Feature Request: interval.includes #299

MichaelLeeHobbs opened this issue Dec 1, 2022 · 0 comments

Comments

@MichaelLeeHobbs
Copy link

/**
 * Returns true if the dt is within the cron range
 * @param {Date|string} [dt=(new Date()).toISOString()] - The date to check or an ISO string that can be parsed by luxon
 * @returns {boolean}
 */
CronExpression.prototype.includes = function (dt) {
  dt ??= (new Date()).toISOString()
  let _dt = DateTime.fromISO(dt).setZone(this._options.tz)
  let {second, minute, hour, dayOfMonth, month, dayOfWeek} = this._fields
  return dayOfMonth.includes(_dt.day) && dayOfWeek.includes(_dt.weekday) && month.includes(_dt.month) && hour.includes(_dt.hour) && minute.includes(_dt.minute) && second.includes(_dt.second)
}
const interval = cronParser.parseExpression('* * 0-8,16-23 ? * MON,TUE,WED,THU,FRI', {tz: 'America/New_York'})
console.log(`Now with in the cron interval? ${interval.includes()}`)
console.log(`5:59 am PDT Los Angeles with in the cron interval? ${interval.includes('2022-12-01T05:59:59-0800')}`)
console.log(`6:00 am PDT Los Angeles with in the cron interval? ${interval.includes('2022-12-01T06:00:00-0800')}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant