Skip to content

Date

Date represents a calendar date (year, month, day) with no time or timezone component.

FactoryReturnsNotes
Date.of(year, month, day)DateAll args Int
Date.today()DateCurrent local date
Date.parse(s: String)Option<Date>Accepts yyyy-MM-dd only; None on invalid input
MethodReturnsNotes
.year()Int
.month()Int
.day()Int
.toDatetime()DatetimeMidnight on that date (local time)
.toString()StringFormat: yyyy-MM-dd 00:00:00 (matches System.debug(Date))
.add(years:, months:, days:)DateAll args optional (default 0), at least one required. Negative values subtract. Month-end clamping applied.
.daysUntil(other: Date)IntWhole calendar days from self to other. Positive when other is later, negative when earlier.
.format(DateFormat)StringFormat the date using a DateFormat value.

Date.parse(s: String) returns Option<Date>. Only the format yyyy-MM-dd is accepted (matching Apex’s Date.valueOf). Any other format returns None.

let d = Date.parse(s: "2024-03-15") // Some(value: 2024-03-15 00:00:00)
let x = Date.parse(s: "15/03/2024") // None

date.add(...) returns a new Date shifted by the given calendar units. All parameters are labeled; any subset may be provided (at least one required). Negative values subtract:

date.add(days: 7) // + 7 days
date.add(months: -1) // - 1 month
date.add(years: 1, months: 2, days: 3)

Month-end clamping: Date.of(year: 2024, month: 1, day: 31).add(months: 1)2024-02-29.

==, !=, <, <=, >, >=. Direct date comparison.