Skip to main content
Cron provides utilities for working with cron expressions and scheduled tasks.

Type Definition

interface Cron {
  readonly _id: 'Cron'
}

Constructors

parse

Parses a cron expression string.
const parse: (expression: string) => Either<CronError, Cron>

make

Creates a Cron from components.
const make: (options: {
  minutes?: string
  hours?: string
  days?: string
  months?: string
  weekdays?: string
}) => Cron

Operations

next

Calculates the next occurrence.
const next: (now: DateTime) => (self: Cron) => DateTime

schedule

Creates a Schedule from a Cron expression.
const schedule: (cron: Cron) => Schedule<DateTime>

match

Checks if a DateTime matches the cron expression.
const match: (date: DateTime) => (self: Cron) => boolean

Example

import { Cron, Effect, Schedule } from "effect"

const program = Effect.gen(function* () {
  const cron = Cron.parse("0 9 * * MON-FRI").pipe(
    Effect.orDie
  )
  
  const schedule = Cron.schedule(cron)
  
  yield* Effect.repeat(
    Effect.log("Running daily task"),
    schedule
  )
})

Build docs developers (and LLMs) love