I’m facing issues with writing every n weeks cron expression where n>1 that is supported by temporal and selected days can be multiple from Sunday through Saturday. Can someone please help me with a solution?
Are you using old cron feature or schedules?
With schedules you can define days of week via ScheduleCalendarSpec
and startAt and endAt times via ScheduleSpec as well as “every x” via ScheduleIntervalSpec.
I’m using temporal schedules but startat and endat only trims the schedule duration it doesn’t guarantee that the start date will be exact same as mentioned. And as 5 digit cron expression doesn’t support every n weeks or interval based schedules so I was looking for @every days with offsets based syntax. Can you provide me a solution using cron expressions itself.
With cron and @every you can define duration (see here).
Cron like 0 10 * * 1-5
should mean like “every 10am monday to friday” if that helps. whats the requirement for this offset?
Yes I’ve been trying to use the same with offsets, but I’m unable to find a cron expression that starts on exactly the same date that is mentioned by user (any future date) as every n days start with the unix epoch. Can you provide an example for a perfect cron expression that works for all cases
Don’t think you can do that with cron expression alone. Im not sure which Temporal sdk you use but maybe something like this helps, at least you could maybe then compare it with your requirements and explain whats the difference/what else is needed so we can understand.
// start time, can for example set to dec 2 2025 (utc)
Instant startTime = ZonedDateTime.of(2025, 12, 2, 0, 0, 0, 0, ZoneId.of("UTC")).toInstant();
// end time, can for example set to dec 4 2025 (utc)
Instant endTime = ZonedDateTime.of(2025, 12, 4, 0, 0, 0, 0, ZoneId.of("UTC")).toInstant();
Schedule schedule =
Schedule.newBuilder()
.setAction(action)
.setSpec(
ScheduleSpec.newBuilder()
.setIntervals(
Arrays.asList(
// we want this schedule to run at 1:03 utc once per day between start and end times
new ScheduleIntervalSpec(Duration.ofHours(24), Duration.ofMinutes(63))))
.setStartAt(startTime)
.setEndAt(endTime)
.build())
.build();
this schedule if started today would have two runs
2025-12-02 UTC 01:03:00.00
2025-12-03 UTC 01:03:00.00