How to use the CronTime function from cron

Find comprehensive JavaScript cron.CronTime code examples handpicked from public code repositorys.

cron.CronTime is a constructor that creates a new object representing a cron time pattern to be used for scheduling jobs.

186
187
188
189
190
191
192
193
194
195
}
if (endDate < startDate) {
  throw new Error('Invalid dates')
}

const cronTime = new CronTime(pattern, timezone)

let continueLoop = true
const dates = []
let cronISODate = new Date(new Date(startDate).getTime() - 1) // start from `startDate` minus 1 millisecond
fork icon58
star icon265
watch icon12

+ 7 other calls in file

129
130
131
132
133
134
135
136
137
138
const cronTime = `0 ${minutes} ${hours} * * ${weekdays}`;
const timeZone = settings.timezone;

try {
  // eslint-disable-next-line no-new
  new CronTime(cronTime, timeZone);
  return { cronTime, timeZone, runOnce };
} catch (error) {
  this.error(`${this.getName()} - getSettingsCronTime - Time = [${cronTime}], Timezone = [${timeZone}] error: ${error}`);
}
fork icon0
star icon0
watch icon1

How does cron.CronTime work?

CronTime is a class in the cron module that provides a way to define and manage cron job schedules by parsing and validating the time-based cron expressions that define the schedule. The class exposes various methods and properties to retrieve and manipulate the cron expression components, such as the minute, hour, day of the month, etc. It also provides methods to determine the next scheduled time and calculate the time interval until the next scheduled occurrence.

157
158
159
160
161
162
163
164
165
166

cronTime(data) {
  let cron = Utils.cronTime(_.defaultTo(data, this.opt("cron")));

  if (cron) {
    let cronTimeStart = new CronTime(cron.start, this.opt("timezone"));
    let cronTimeStop = new CronTime(cron.stop, this.opt("timezone"));

    this.start_job.setTime(cronTimeStart);
    this.stop_job.setTime(cronTimeStop);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

90
91
92
93
94
95
96
97
98
99
    message = `*Sunrise*🌄: ${astroData['sunrise']}
    *Sunset*🌇: ${astroData['sunset']}`.split("\n").map(s=>s.trim()).join("\n")
    break;
case "!refreshsched":
    descriptionData = getDescription(chat.description)
    job.setTime(new CronTime(`${timeTrigger} * * *`))
    message = `New timer set for ${stringTimeFormatter(timeTrigger)}, Clock has started`
    break;
case "!startsched":
    console.log("schedule started")
fork icon0
star icon0
watch icon0

Ai Example

1
2
const CronTime = require("cron").CronTime;
const time = new CronTime("* * * * *"); // runs every minute