Time.PlainDate
type Time.PlainDateTime.PlainDate is a calendar date with no time and no zone, such as a birthday. It renders as 2026-10-22.
Details
- A
Time.PlainDateis immutable and always a real date. Functions that change it, such as Time.add and Time.with, return a new value. - Two dates are
==when their year, month, and day are. The comparison operators such as<do not apply; Time.compare orders dates. - Time.plainDate panics on a date that does not exist, and Time.parsePlainDate returns an
Errfor it. Use the parser for text from outside the program. - A duration's hours and smaller units count as whole 24-hour days on a date, and the rest is dropped.
Basic examples
Build a date from its fields, or parse one from text:
Time.plainDate(2026, 10, 22) // 2026-10-22
Time.parsePlainDate("2026-10-22") // Ok{ value: 2026-10-22 }
Time.parsePlainDate("2026-10-22").unwrap() // 2026-10-22Take the date a Time.ZonedDateTime shows in its zone. The same moment can be a different date in another zone:
let late = Time.parseZoned("2026-10-22T23:30:00+02:00[Europe/Stockholm]").unwrap()
late.toPlainDate() // 2026-10-22
late.withZone(Time.utc).toPlainDate() // 2026-10-22
let tokyo = Time.zone("Asia/Tokyo").unwrap()
late.withZone(tokyo).toPlainDate() // 2026-10-23Today's date depends on the zone you ask in, so take the current time in a zone and then its date:
let today = Time.nowIn(Time.localZone()).toPlainDate()
println(today.year() >= 2026)trueScope
Fields
The date fields read a Time.PlainDate through .:
let d = Time.plainDate(2026, 10, 22)
d.year() // 2026
d.month() // 10
d.day() // 22
d.dayOfWeek() // 4
d.dayOfYear() // 295
d.weekOfYear() // 43
d.daysInMonth() // 31
d.daysInYear() // 365
d.inLeapYear() // falseChanging a date
Time.with replaces fields and constrains the day to the month, and Time.add and Time.subtract move by a duration:
let d = Time.plainDate(2026, 10, 22)
d.with(day: 1) // 2026-10-01
d.with(month: 2, day: 31) // 2026-02-28
d.add(Time.duration(weeks: 1)) // 2026-10-29
d.subtract(Time.duration(years: 1)) // 2025-10-22
d.add(Time.duration(days: 1, hours: 30)) // 2026-10-24Measuring between dates
Time.until counts days by default, and larger units on request:
let start = Time.plainDate(2026, 1, 1)
let end = Time.plainDate(2026, 10, 22)
start.until(end) // P294D
start.until(end, largestUnit: Time.Month{}) // P9M21D
end.until(start) // -P294D
Time.compare(start, end) // -1Text and JSON
Interpolation, String.from, and Json.stringify write the ISO 8601 form. Time.parsePlainDate also takes the date out of a longer string with a time and zone:
let d = Time.plainDate(2026, 10, 22)
"due ${d}" // "due 2026-10-22"
String.from(d) // "2026-10-22"
Json.stringify([d]) // "[\"2026-10-22\"]"
Time.parsePlainDate("2026-10-22T14:03:05+02:00[Europe/Stockholm]") // Ok{ value: 2026-10-22 }
Time.plainDate(-44, 3, 15) // -000044-03-15
Time.plainDate(12345, 1, 1) // +012345-01-01Records, maps, and unions
A date is a field like any other, and a map key:
type Person = { name: string, born: Time.PlainDate }
let ann = Person{ name: "Ann", born: Time.plainDate(1990, 7, 14) }
ann // Person{ name: "Ann", born: 1990-07-14 }
Json.stringify(ann) // "{\"name\":\"Ann\",\"born\":\"1990-07-14\"}"
let holidays = Map{ Time.plainDate(2026, 12, 25): "Christmas" }
holidays[Time.plainDate(2026, 12, 25)] // "Christmas"is narrows a union of time types to Time.PlainDate:
func describe(t: Time.Dated): string {
if t is Time.PlainDate { "the whole day ${t}" } else { "a moment on ${t.toPlainDate()}" }
}
println(describe(Time.plainDate(2026, 10, 22)))
println(describe(Time.plainDateTime(Time.plainDate(2026, 10, 22), Time.plainTime(9))))the whole day 2026-10-22
a moment on 2026-10-22Adding a time
Time.plainDateTime joins a date and a time, and Time.toZoned then places it in a zone:
let d = Time.plainDate(2026, 10, 22)
let meeting = Time.plainDateTime(d, Time.plainTime(9, 30))
meeting // 2026-10-22T09:30:00
meeting.toZoned(Time.utc) // 2026-10-22T09:30:00+00:00[UTC]Applications
A person's age in whole years on a given day:
func age(born: Time.PlainDate, on: Time.PlainDate): int {
born.until(on, largestUnit: Time.Year{}).part(Time.Year{})
}
let born = Time.plainDate(1990, 7, 14)
age(born, Time.plainDate(2026, 7, 13)) // 35
age(born, Time.plainDate(2026, 7, 14)) // 36The last day of a month:
let d = Time.plainDate(2024, 2, 10)
d.with(day: d.daysInMonth()) // 2024-02-29The working days of two weeks, Monday to Friday:
let start = Time.plainDate(2026, 12, 21)
let workdays = Iter.range(0, 14)
| Iter.map({ start.add(Time.duration(days: x)) })
| Iter.filter({ x.dayOfWeek() <= 5 })
| Iter.toArray()
println(workdays.length())
println(workdays[workdays.length() - 1])10
2027-01-01Sort dates with Time.compare, and group them by month, keyed by the first day of each month:
let dates = [Time.plainDate(2026, 3, 1), Time.plainDate(2025, 12, 24), Time.plainDate(2026, 3, 9)]
dates.sortWith(Time.compare) // [2025-12-24, 2026-03-01, 2026-03-09]
let byMonth = dates.groupBy({ x.with(day: 1) })
byMonth[Time.plainDate(2026, 3, 1)] // [2026-03-01, 2026-03-09]
byMonth.keys() // [2026-03-01, 2025-12-01]Properties and relations
A date parses back from the text it renders as:
let d = Time.plainDate(2026, 10, 22)
assert(Time.parsePlainDate(String.from(d)).unwrap() == d)
let bc = Time.plainDate(-44, 3, 15)
assert(Time.parsePlainDate("${bc}").unwrap() == bc)Adding the days Time.until counts gets back to the later date:
let a = Time.plainDate(2024, 2, 10)
let b = Time.plainDate(2026, 10, 22)
assert(a.add(a.until(b)) == b)Time.compare is antisymmetric:
let a = Time.plainDate(2024, 2, 10)
let b = Time.plainDate(2026, 10, 22)
assert(Time.compare(a, b) == -Time.compare(b, a))A date joined with a time and taken apart again is the same date:
let d = Time.plainDate(2026, 10, 22)
assert(Time.plainDateTime(d, Time.plainTime(23, 59)).toPlainDate() == d)Possible issues
A date that does not exist panics in Time.plainDate. Text from outside goes through Time.parsePlainDate, which returns an Err instead:
Time.plainDate(2026, 2, 29) // panic: Time.plainDate: no such date: 2026-02-29
Time.parsePlainDate("2026-02-29") // Err{ error: Error{ message: "no such date: 2026-02-29" } }A string that ends in Z with no zone in brackets names a moment, not a wall-clock date, so the parser refuses it:
Time.parsePlainDate("2026-10-22T14:03:05Z") // Err{ error: Error{ message: "Z names an exact time; a plain value needs a wall-clock time" } }The ISO week number belongs to the week's year, which differs from the date's year around New Year. Pair weekOfYear with yearOfWeek, not year:
let ny = Time.plainDate(2027, 1, 1)
ny.weekOfYear() // 53
ny.year() // 2027
ny.yearOfWeek() // 2026< does not compare dates, and Array.sort does not sort them. Use Time.compare:
let a = Time.plainDate(2026, 3, 1)
let b = Time.plainDate(2026, 4, 1)
let early = a < b
// error: < applies to a single numeric kind, string, or a named primitive type over one, not to Time.PlainDate
let sorted = [b, a].sort()
// error: T would be Time.PlainDate, which is not a member of its bound OrderedA date has no time of day. The time fields and Time.round do not take it, and Time.toZoned needs a Time.PlainDateTime:
let d = Time.plainDate(2026, 3, 1)
let h = Time.hour(d) // error: cannot use Time.PlainDate where Time.Timed is expected
let r = Time.round(d, Time.Day{})
// error: T would be Time.PlainDate, which is not a member of its bound Time.Instant | Time.Timed
let z = Time.toZoned(d, Time.utc)
// error: cannot use Time.PlainDate where Time.PlainDateTime is expectedTime.with and Time.until panic on a time field or unit:
let d = Time.plainDate(2026, 3, 1)
let april = Time.plainDate(2026, 4, 1)
d.with(hour: 3) // panic: Time.with: a date has no time fields
d.until(april, largestUnit: Time.Hour{}) // panic: Time.until: a date has no hoursHours shorter than a day are dropped, so small steps add up to nothing:
let d = Time.plainDate(2026, 1, 1)
d.add(12h).add(12h) // 2026-01-01
d.add(24h) // 2026-01-02See also
Time.plainDate · Time.parsePlainDate · Time.toPlainDate · Time.plainDateTime · Time.PlainDateTime · Time.PlainTime · Time.ZonedDateTime · Time.Dated · Time.with · Time.add · Time.until · Time.compare
