How does Date promote to DateTime in EK9?
← Date, Time, and Duration · Ref: Q552
Date automatically promotes to DateTime via the #^ operator. DateTime can also be decomposed back into its Date and Time parts.
DATE TO DATETIME PROMOTION
The #^ promote operator converts Date to DateTime (midnight UTC):
birthday <- 1971-02-01 birthdayDT <- #^ birthday birthdayDT is 1971-02-01T00:00:00Z
Automatic promotion also occurs when assigning Date to a DateTime variable:
dtValue as DateTime: birthday dtValue is 1971-02-01T00:00:00Z
Or via the DateTime(Date) constructor:
explicit <- DateTime(birthday)
DATETIME TO DATE AND TIME EXTRACTION
The date() method extracts the Date portion:
myDate <- myDateTime.date()
The time() method extracts the Time portion:
myTime <- myDateTime.time()
Alternatively, use extraction operators:
#< extracts the Date (lower/coarser part) #> extracts the Time (upper/finer part) datePart <- #< myDateTime timePart <- #> myDateTime
MILLISECOND TO DURATION
Similar promotion: Millisecond promotes to Duration via #^:
ms <- 5000ms dur <- #^ ms dur is PT5S
See Q25 for the promote operator in general. See Q31 for Date and Time basics. See Q92 for DateTime with timezones. See Q554 for extracting DateTime components.
Example
defines module qa.date.promote.datetime defines program DatePromoteDateTimeDemo() stdout <- Stdout() birthday <- 1971-02-01 // Explicit promote operator promoted <- #^ birthday stdout.println(`Promoted: ${promoted}`) // Assignment promotion dtValue as DateTime: birthday stdout.println(`Assigned: ${dtValue}`) // Constructor promotion explicit <- DateTime(birthday) stdout.println(`Constructor: ${explicit}`) // All three are equivalent require promoted == dtValue require dtValue == explicit // DateTime decomposition meeting <- 2024-06-15T10:30:00Z // Method extraction datePart <- meeting.date() timePart <- meeting.time() stdout.println(`Date part: ${datePart}`) stdout.println(`Time part: ${timePart}`) // Operator extraction dateExtract <- #< meeting timeExtract <- #> meeting stdout.println(`#< extract: ${dateExtract}`) stdout.println(`#> extract: ${timeExtract}`) require datePart == dateExtract require timePart == timeExtract // Millisecond to Duration promotion ms <- 5000ms dur <- #^ ms stdout.println(`5000ms promoted: ${dur}`) // Round-trip: Date -> DateTime -> Date original <- 2024-03-15 asDT as DateTime: original backToDate <- asDT.date() require backToDate == original stdout.println(`Round-trip: ${original} -> ${asDT} -> ${backToDate}`)
Common mistakes
E50060 — DateTime has no toDate() or toTime() methods. Use date() and time() to extract the Date and Time components. See ek9 -h E50060 for details.
Incorrect:
datePart <- meeting.toDate() timePart <- meeting.toTime()
Correct:
datePart <- meeting.date() timePart <- meeting.time()
Other ways to ask this
- How do I convert a Date to DateTime?
- What does the #^ promote operator do with Date?
- How do I extract Date and Time from DateTime?
Coming from another language?
Java: LocalDate.atStartOfDay(ZoneOffset.UTC) to convert Date to DateTime. LocalDateTime.toLocalDate() and toLocalTime() for extraction. Python: datetime.combine(date, time.min) for promotion. Go: no separate Date type. Rust: NaiveDate with_hms() methods. EK9: #^ promotes Date to DateTime automatically, date() and time() extract parts, #< and #> extraction operators.
Keywords: extract, date, time, constructor, timezone, promote, date(), time(), convert, datetime, decompose, duration