Calculate the time gap between two scheduled events.

← Operators and Expressions · Ref: Q1202

EK9 Date supports operator - with another Date, returning a Duration. Use Duration comparison to check if the gap meets a threshold.

DATE SUBTRACTION

  gap <- eventB.eventDate - eventA.eventDate   returns Duration

DURATION COMPARISON

  threshold <- P30D
  if gap > threshold
    stdout.println("Events are more than 30 days apart")

Component access on Duration:

  gap.days()    number of days
  gap.months()  number of months

See Q539 for duration between dates. See Q1188 for duration operations. See Q1186 for date comparison.

Example

#!ek9
defines module qa.operators.durationbetweenevents

  defines record

    ScheduledEvent
      eventName as String: String()
      eventDate as Date: Date()

      ScheduledEvent() as pure
        ->
          theName as String
          theDate as Date
        this.eventName :=: theName
        this.eventDate :=: theDate

      default operator

  defines program

    DurationBetweenEventsDemo()
      stdout <- Stdout()

      // === CREATE EVENTS ===

      launch <- ScheduledEvent("Product Launch", 2024-01-15)
      milestone <- ScheduledEvent("Q2 Milestone", 2024-06-30)
      review <- ScheduledEvent("Annual Review", 2024-12-31)

      // === CALCULATE GAPS ===

      launchToMilestone <- milestone.eventDate - launch.eventDate
      stdout.println(`Launch to Milestone: ${launchToMilestone}`)
      stdout.println(`Days: ${launchToMilestone.days()}`)

      milestoneToReview <- review.eventDate - milestone.eventDate
      stdout.println(`Milestone to Review: ${milestoneToReview}`)

      // === COMPARE DURATIONS ===

      if launchToMilestone < milestoneToReview
        stdout.println("First half is shorter than second half")

      // === CHECK AGAINST THRESHOLD ===

      threshold <- P90D
      if launchToMilestone > threshold
        stdout.println(`Launch to milestone exceeds ${threshold}`)

      // === TOTAL SPAN ===

      totalSpan <- review.eventDate - launch.eventDate
      stdout.println(`Total span: ${totalSpan}`)

Common mistakes

E50060 — EK9 Date has no between() method (and no ChronoUnit) — subtract two Dates with the - operator to get a Duration. See ek9 -h E50060 for details.

Incorrect:

      stdout.println(`Launch to milestone exceeds ${launch.eventDate.between(review.eventDate)}`)

Correct:

      stdout.println(`Launch to milestone exceeds ${threshold}`)
Other ways to ask this
  • Subtract two dates to find the duration between events.
  • I need to compute how long between a project start and its milestone.
  • In Java I'd use ChronoUnit.DAYS.between(). What is the EK9 equivalent?
  • Given two event dates, find the duration gap and compare it to a threshold.

Coming from another language?

Java: ChronoUnit.DAYS.between(d1, d2) or Period.between(). Python: (d2 - d1).days. Rust: d2.signed_duration_since(d1). Go: t2.Sub(t1). EK9: eventB.eventDate - eventA.eventDate gives Duration directly, then compare with < > operators.

Keywords: between, compare, date, days, duration, events, gap, schedule, subtract, threshold