Grav v1.14.0

DateTime

import "std/sys" (included in import "std") · namespace std.sys

Calendar dates and times in the local time zone. A DateTime is a moment, stored as Unix seconds; its parts (year, month, day, hour, ...) are computed in local time. Values cannot change: addDays and friends return a new DateTime.

grav
import "std"

fn main() {
    let d = DateTime.of(2024, 2, 28, 9, 30, 0)
    print(d)
    print(d.addDays(1).date())
    print(d.addDays(2).date())
    print(DateTime.isLeapYear(2024))
    print(DateTime.daysInMonth(2023, 2))
    print(d.weekday())                       // 1 = Monday ... 7 = Sunday
    print(d.dayOfYear())
    print(d.format("%d/%m/%Y %H:%M"))
    let start = DateTime.of(2024, 1, 1)
    print(start.daysUntil(DateTime.of(2024, 12, 25)))
    print(start < d)
}
Output
2024-02-28 09:30:00
2024-02-29
2024-03-01
true
28
3
59
28/02/2024 09:30
359
true
  • DateTime.now() is the current moment; fromUnix(seconds) and unix() convert to and from Unix time.
  • format takes C strftime patterns: %Y year, %m month, %d day, %H hour, %M minute, %S second, %A weekday name, %B month name, and more.
  • date() is YYYY-MM-DD, time() is HH:MM:SS, and print shows both.
  • ==, <, > and the rest compare moments.

Parsing and calendar arithmetic

DateTime.parse reads YYYY-MM-DD with an optional time (after a space or T), fractional seconds (09:30:15.25) and a zone (Z, +03:00, -0500; without one the time is local), or any layout given as a strptime pattern. DateTime.tryParse is the same but gives null for text that is not a date.

A DateTime is a moment to the microsecond: micros() is the part past the second, unixMicros() the whole of it, and DateTime.fromUnix(seconds, micros) makes one. now(), of() and the arithmetic keep the microseconds as they are (0 unless a parsed time had a fraction), print, toString() and iso() show them as .ffffff only when they are not 0, and the comparisons use them. addMonths and addYears keep the day where they can and use the month's last day where they cannot. plus, minus and until work with Duration.

grav
import "std"

fn main() {
    let d = DateTime.parse("2024-01-31 09:30")
    print(d.iso())
    print(d.addMonths(1))
    print(DateTime.parse("2024-02-29").addYears(1).date())
    print(DateTime.parse("15/03/2024", "%d/%m/%Y").date())
    print(d.plus(Duration.parse("90m")).time())
    print(d.until(DateTime.parse("2024-02-01 10:00")))
    print(d.startOfDay())
}
Output
2024-01-31T09:30:00
2024-02-29 09:30:00
2025-02-28
2024-03-15
11:00:00
1d30m
2024-01-31 00:00:00

UTC and HTTP dates

isoUtc() writes RFC 3339 in UTC (1994-11-06T08:49:37Z), as JSON APIs and logs do, and parse reads it back. isoLocal() writes local time with the zone's offset (2026-11-21T15:00:00+03:00). formatUtc(pattern) is format in UTC, and ofUtc(year, month, day, ...) builds a moment from UTC fields. httpDate() and DateTime.parseHttpDate(text) handle the dates of HTTP headers (Last-Modified, Expires, cookies), including the two older forms some clients still send; parseHttpDate gives null for anything else.

grav
import "std"

fn main() {
    let d = DateTime.ofUtc(1994, 11, 6, 8, 49, 37)
    print(d.isoUtc())
    print(d.httpDate())
    print(DateTime.parseHttpDate("Sunday, 06-Nov-94 08:49:37 GMT").unix() == d.unix())
}
Output
1994-11-06T08:49:37Z
Sun, 06 Nov 1994 08:49:37 GMT
true

API

class DateTime

Constructors

  • new DateTime(unixSeconds: long)

Static members

  • static fn now() -> DateTime — The current moment.
  • static fn fromUnix(seconds: long) -> DateTime — The moment seconds after 1970-01-01 00:00 UTC.
  • static fn fromUnix(seconds: long, micros: int) -> DateTime — The moment seconds and micros microseconds after 1970-01-01 00:00 UTC.
  • static fn of(year: int, month: int, day: int, hour: int, minute: int, second: int) -> DateTime — From calendar fields (local time); out-of-range fields roll over (Jan 32 -> Feb 1).
  • static fn of(year: int, month: int, day: int) -> DateTime — Midnight (local time) of the given date.
  • static fn isLeapYear(year: int) -> bool — Whether year has a February 29.
  • static fn daysInMonth(year: int, month: int) -> int — How many days the month has (month 1..12).
  • static fn parse(text: string) -> DateTime — Parses "2024-02-28", "2024-02-28 09:30", "2024-02-28 09:30:15" or the ISO "2024-02-28T09:30:15", with optional fractional seconds (".25") and zone ("Z", "+03:00", "-0500"; local time without one); ValueError for anything else.
  • static fn tryParse(text: string) -> DateTime — As parse, but null for text that is not a date.
  • static fn parse(text: string, pattern: string) -> DateTime — Parses text with a strptime pattern such as "%d/%m/%Y %H:%M" (local time); ValueError when it does not fit.
  • static fn ofUtc(year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0) -> DateTime — From UTC calendar fields (out-of-range fields roll over).
  • static fn parseHttpDate(text: string) -> DateTime — An HTTP date (RFC 9110, and the older RFC 850 and asctime forms); null when it is none.

Methods

  • fn unix() -> long — Seconds since 1970-01-01 00:00 UTC.
  • fn micros() -> int — The microseconds past the second, 0..999999.
  • fn unixMicros() -> long — Microseconds since 1970-01-01 00:00 UTC.
  • fn year() -> int — The year (local time).
  • fn month() -> int — The month, 1..12 (local time).
  • fn day() -> int — The day of the month, 1..31 (local time).
  • fn hour() -> int — The hour, 0..23 (local time).
  • fn minute() -> int — The minute, 0..59.
  • fn second() -> int — The second, 0..59.
  • fn weekday() -> int — ISO weekday: 1 = Monday … 7 = Sunday.
  • fn dayOfYear() -> int — The day of the year, 1..366.
  • fn format(fmt: string) -> string — strftime patterns, e.g. "%Y-%m-%d %H:%M".
  • fn date() -> string — The date as "YYYY-MM-DD".
  • fn time() -> string — The time of day as "HH:MM:SS".
  • fn toString() -> string — "YYYY-MM-DD HH:MM:SS", with ".ffffff" when the microseconds are not 0.
  • fn addSeconds(s: long) -> DateTime — A moment s seconds later (earlier when negative).
  • fn addMinutes(m: int) -> DateTime — A moment m minutes later.
  • fn addHours(hours: int) -> DateTime — A moment hours hours later.
  • fn addDays(days: int) -> DateTime — A moment days days later (24-hour days).
  • fn secondsUntil(other: DateTime) -> long — Seconds from this instant to other (negative if other is earlier).
  • fn daysUntil(other: DateTime) -> long — Whole days from this moment to other (negative when other is earlier).
  • fn operator==(o: DateTime) -> bool — Whether both are the same moment.
  • fn operator<(o: DateTime) -> bool — Whether this moment is earlier than o.
  • fn operator<=(o: DateTime) -> bool — Whether this moment is not later than o.
  • fn operator>(o: DateTime) -> bool — Whether this moment is later than o.
  • fn operator>=(o: DateTime) -> bool — Whether this moment is not earlier than o.
  • fn iso() -> string — "YYYY-MM-DDTHH:MM:SS" (ISO 8601, local time), with ".ffffff" when the microseconds are not 0.
  • fn isoUtc() -> string — "YYYY-MM-DDTHH:MM:SSZ" in UTC (RFC 3339, as JSON APIs and logs write it), with ".ffffff" when the microseconds are not 0. DateTime.parse reads it back.
  • fn isoLocal() -> string — ISO 8601 in local time with the zone's offset ("2026-11-21T15:00:00+03:00").
  • fn formatUtc(fmt: string) -> string — strftime patterns in UTC ("%Y-%m-%d %H:%M").
  • fn utcOffsetSeconds() -> long — The local zone's offset from UTC at this moment, in seconds (east positive).
  • fn httpDate() -> string — As an HTTP date: "Sun, 06 Nov 1994 08:49:37 GMT" (Last-Modified, Expires, cookies).
  • fn startOfDay() -> DateTime — Midnight at the start of this day.
  • fn addMonths(months: int) -> DateTime — The same day and time months later (or earlier when negative); a day that the target month lacks becomes its last day (Jan 31 + 1 month = Feb 28 or 29).
  • fn addYears(years: int) -> DateTime — The same date years later (Feb 29 becomes Feb 28 in a non-leap year).
  • fn plus(d: Duration) -> DateTime — This moment plus a Duration.
  • fn minus(d: Duration) -> DateTime — This moment minus a Duration.
  • fn until(other: DateTime) -> Duration — The time from this moment until other (negative when other is earlier; to the millisecond).
  • fn isSameDay(other: DateTime) -> bool — Whether both are on the same calendar day.

Edit this page on GitHub