Skip to content

DateFormat

DateFormat is a built-in opaque type that describes how to render a Date or Datetime as a String. Values are created via named factory constructors or the custom escape hatch. DateFormat values may be stored in variables and passed around freely.

Each constructor produces a locale-specific pattern using en_US conventions.

ConstructorExample outputPattern (en_US)
DateFormat.yMd()3/15/2024M/d/y
DateFormat.yMMMd()Mar 15, 2024MMM d, y
DateFormat.yMMMMd()March 15, 2024MMMM d, y
DateFormat.yMMMMEEEEd()Friday, March 15, 2024EEEE, MMMM d, y
DateFormat.yMMMEd()Fri, Mar 15, 2024EEE, MMM d, y
DateFormat.MMMd()Mar 15MMM d
DateFormat.MMMMd()March 15MMMM d
DateFormat.MMMMEEEEd()Friday, March 15EEEE, MMMM d
DateFormat.yMMM()Mar 2024MMM y
DateFormat.yMMMM()March 2024MMMM y
DateFormat.yM()3/2024M/y
DateFormat.y()2024y
DateFormat.jm()10:30 AMh:mm a
DateFormat.jms()10:30:00 AMh:mm:ss a
DateFormat.Hm()10:30HH:mm
DateFormat.Hms()10:30:00HH:mm:ss

DateFormat.custom(pattern: String) accepts a raw Java SimpleDateFormat pattern string.

dt.format(DateFormat.custom(pattern: "yyyy-MM-dd")) // 2024-03-15
d.format(DateFormat.custom(pattern: "dd/MM/yyyy")) // 15/03/2024

The pattern: argument is required and must be labeled.

let dt = Datetime.of(year: 2024, month: 3, day: 15, hour: 10, minute: 30, second: 0)
let d = Date.of(year: 2024, month: 3, day: 15)
debug dt.format(DateFormat.yMMMd()) // Mar 15, 2024
debug dt.format(DateFormat.yMMMMEEEEd()) // Friday, March 15, 2024
debug dt.format(DateFormat.Hms()) // 10:30:00
debug dt.format(DateFormat.custom(pattern: "yyyy-MM-dd")) // 2024-03-15
debug d.format(DateFormat.MMMd()) // Mar 15
debug d.format(DateFormat.yMMM()) // Mar 2024

format always returns String. Format patterns are programmer-written source code, not user input, so failures are considered programmer error rather than something to handle at runtime.