Show me how to use ++ on a Date to advance one day at a time, iterating through a week starting from a known date.

← Operators and Expressions · Ref: Q1261

Date supports the ++ and -- operators. Each ++ advances the date by ONE DAY; each -- moves it back by ONE DAY. Like all ++/-- operators in EK9, these are STATEMENT-ONLY and modify the variable in place.

ADVANCING ONE DAY AT A TIME

  startOfWeek <- 2025-03-10
  current <- Date(startOfWeek)
  current++   // 2025-03-11
  current++   // 2025-03-12
  current++   // 2025-03-13

The Date arithmetic understands month boundaries — incrementing 2025-03-31 by ++ produces 2025-04-01, not '2025-03-32'.

ITERATING A WEEK

  startOfWeek <- 2025-03-10
  current <- Date(startOfWeek)
  daysToShow <- 7
  daysShown <- 0
  while daysShown < daysToShow
    stdout.println(`Day ${daysShown + 1}: ${current}`)
    current++
    daysShown++

This prints seven consecutive dates without ever touching a Duration. The ++ operator IS the 'add one day' API for Date.

GOING BACKWARDS

  todayLike <- 2025-03-15
  yesterday <- Date(todayLike)
  yesterday--   // 2025-03-14

LEAP YEAR AND MONTH BOUNDARIES

  monthEnd <- 2024-02-28
  next <- Date(monthEnd)
  next++   // 2024-02-29 (2024 is a leap year)
  next++   // 2024-03-01

The Date type handles leap years and month lengths automatically. You never need to compute the next valid date yourself.

See Q1259 for Integer ++/--. See Q1260 for Float ++/--. See Q1262 for Enumeration ++/--. See Q241 for mutation operators overview.

Example

defines module qa.operators.incrementdate

  defines program

    IncrementDateDemo()
      stdout <- Stdout()

      startOfWeek <- 2025-03-10
      current <- Date(startOfWeek)
      daysToShow <- 7
      daysShown <- 0

      while daysShown < daysToShow
        stdout.println(`Day ${daysShown + 1}: ${current}`)
        current++
        daysShown++

      yesterday <- 2025-03-15
      previousDay <- Date(yesterday)
      previousDay--
      stdout.println(`Yesterday: ${previousDay}`)

Common mistakes

E50060 — EK9 Date does not have an addDays method. The compiler cannot resolve the method call. Use the ++ operator to advance by one day, or += Duration() for larger steps. See ek9 -h E50060 for details.

Incorrect:

current.addDays(1)

Correct:

current++
Other ways to ask this
  • How does ++ work on a Date in EK9?
  • Increment a Date by one day using ++ in EK9.
  • Walk forward through dates one day at a time without using a Duration.
  • Show me Date ++ and -- in action.

Coming from another language?

Java: LocalDate.plusDays(1) — method call, no operator. Kotlin: same as Java. Python: date + timedelta(days=1) — uses arithmetic with a delta. Rust: NaiveDate::succ_opt() — method returning Option. C#: DateTime.AddDays(1) — method call. JavaScript: setDate(getDate() + 1) — manual arithmetic. EK9: Date supports ++ directly — most concise way to step through days.

Keywords: increment, calendar, ++, --, next day, Date, iterate days