datetime, date, time

Luma provides three distinct types for working with dates and times. All formats are human-readable by default — no T or Z needed.

Core Types

dt: datetime = datetime.now()  // Date + time
d: date = date.now()           // Date only
t: time = time.now()           // Time only

Creation

datetime

// Current timestamp
dt: datetime = datetime.now()

// From components
dt: datetime = datetime({"year": 2024, "month": 12, "day": 25, "hour": 14, "minute": 30, "second": 0})

// From string (default format: YYYY-MM-DD HH:MM:SS)
dt: datetime = datetime.parse("2024-12-25 14:30:00")

// From Unix timestamp
dt: datetime = datetime.from_unix(1735122600)

datetime.parse() also accepts:

  • "2024-12-25 14:30" (without seconds)
  • "2024-12-25" (date only, time defaults to 00:00:00)
  • "2024-12-25T14:30:00Z" (ISO 8601, for API interop)

date

// Current date
d: date = date.now()

// From components
d: date = date({"year": 2024, "month": 12, "day": 25})

// From string (default format: YYYY-MM-DD)
d: date = date.parse("2024-12-25")

time

// Current time
t: time = time.now()

// From components
t: time = time({"hour": 14, "minute": 30, "second": 0})

// From string (default format: HH:MM:SS)
t: time = time.parse("14:30:00")
t: time = time.parse("14:30")    // without seconds

Component Access

dt: datetime = datetime({"year": 2024, "month": 12, "day": 25, "hour": 14, "minute": 30, "second": 0})

// Date components
dt.year()    // 2024
dt.month()   // 12
dt.day()     // 25

// Time components
dt.hour()    // 14
dt.minute()  // 30
dt.second()  // 0

// Same for date type (year, month, day only)
d: date = date.now()
d.year()     // 2024
d.month()    // 12
d.day()      // 25

// Same for time type (hour, minute, second only)
t: time = time.now()
t.hour()     // 14
t.minute()   // 30
t.second()   // 0

Type Conversion

dt: datetime = datetime.now()

// Extract parts from datetime
d: date = dt.to_date()       // Get date part
t: time = dt.to_time()       // Get time part

// Combine date + time into datetime
combined: datetime = d.combine(t)

// Unix timestamp (datetime only)
timestamp: int = dt.unix()

Arithmetic

All three types support .add() with a map of units.

// Date arithmetic (supports: years, months, weeks, days)
tomorrow: date = date.now().add({"days": 1})
next_month: date = date.now().add({"months": 1})

// Time arithmetic (supports: hours, minutes, seconds)
// Wraps around midnight automatically
later: time = time.now().add({"hours": 2})
late: time = time.parse("23:30:00")
wrapped: time = late.add({"hours": 2})  // 01:30:00

// DateTime arithmetic (supports all units)
future: datetime = datetime.now().add({"days": 1, "hours": 2})
next_week: datetime = datetime.now().add({"weeks": 1})

Comparisons

All three types support comparison operators and methods.

// Operators: <, >, ==, !=, <=, >=
d1: date = date.parse("2024-12-25")
d2: date = date.parse("2024-12-26")
d1 < d2   // true
d1 == d2  // false

t1: time = time.parse("09:00:00")
t2: time = time.parse("17:00:00")
t1 < t2   // true

dt1: datetime = datetime.parse("2024-12-25 14:30:00")
dt2: datetime = datetime.parse("2024-12-26 10:00:00")
dt1 < dt2  // true

// Methods
dt1.is_before(dt2)  // true
dt1.is_after(dt2)   // false

Formatting

// datetime
dt: datetime = datetime.parse("2024-12-25 14:30:00")
dt.to_string()                    // "2024-12-25 14:30:00"
dt.to_iso_string()                // "2024-12-25T14:30:00Z"
dt.format("DD/MM/YYYY HH:mm")    // "25/12/2024 14:30"

// date
d: date = date.parse("2024-12-25")
d.to_string()                     // "2024-12-25"
d.format("DD/MM/YYYY")            // "25/12/2024"

// time
t: time = time.parse("14:30:00")
t.to_string()                     // "14:30:00"
t.format("HH:mm")                 // "14:30"

Format tokens: YYYY (4-digit year), YY (2-digit year), MM (month), DD (day), HH (hour, 24h), mm (minute), ss (second).

Utility Methods

datetime

dt: datetime = datetime.now()
dt.is_today()       // true/false
dt.is_past()        // true/false
dt.is_future()      // true/false
dt.is_weekend()     // true/false
dt.day_of_week()    // 1 (Monday) to 7 (Sunday)
dt.days_in_month()  // 28-31

date

d: date = date.now()
d.is_today()        // true/false
d.is_past()         // true/false
d.is_future()       // true/false
d.is_weekend()      // true/false
d.day_of_week()     // 1 (Monday) to 7 (Sunday)
d.days_in_month()   // 28-31

time

t: time = time.now()
t.is_am()           // true if before noon
t.is_pm()           // true if noon or later

Real-World Examples

// Age calculation
birthday: date = date({"year": 1990, "month": 5, "day": 15})
today: date = date.now()
age: int = today.year() - birthday.year()
print("Age: ~" + age.to_str())

// Business hours check
work_start: time = time.parse("09:00")
work_end: time = time.parse("17:00")
now_time: time = time.now()
is_working: bool = now_time.is_after(work_start)
print("At work: " + is_working.to_str())

// Event duration
event_start: datetime = datetime.parse("2025-01-10 09:00:00")
event_end: datetime = datetime.parse("2025-01-10 17:30:00")
duration_hours: int = (event_end.unix() - event_start.unix()) / 3600
print("Duration: " + duration_hours.to_str() + " hours")

// Combine date and time
meeting_date: date = date.parse("2025-03-15")
meeting_time: time = time.parse("10:00")
meeting: datetime = meeting_date.combine(meeting_time)
print("Meeting: " + meeting.to_string())

// Logging with timestamp
print("${datetime.now().to_string()}: Application started")
Last updated on