Skip to content
Halyard
Project
Source code on Codeberg (opens in new tab)

Search

Menu

DateTime Module

Date and time operations with UTC timezone support.

The DateTime module provides functions for creating, parsing, formatting, and manipulating date and time values. All operations use UTC timezone. DateTimes are opaque native objects backed by the chrono crate.

Common patterns

halyard
import DateTime

-- Get current time
DateTime.now () |> DateTime.format |> IO.println
-- "2024-02-20T14:30:45Z"

-- Parse and manipulate
"2024-01-15T10:00:00Z"
    |> DateTime.parse
    |> Maybe.map (DateTime.addDays 7)
    |> Maybe.map (DateTime.addHours 2)
    |> Maybe.map DateTime.format
-- Just "2024-01-22T12:00:00Z"

-- Create from components
case DateTime.fromYmd 2024 6 15 of
    Ok dt -> DateTime.format dt
    Err _ -> "invalid"
-- "2024-06-15T00:00:00+00:00"

-- Custom formatting
DateTime.now ()
    |> DateTime.formatCustom "%Y-%m-%d %H:%M"
-- "2024-02-20 14:30"

-- Calendar operations
DateTime.now ()
    |> DateTime.startOfMonth
    |> DateTime.addMonths 1
    |> DateTime.format
-- "2024-03-01T00:00:00+00:00"

Return types

Functions that can fail return Result DateTime DateTimeError:

  • fromParts : Int -> Int -> Int -> Int -> Int -> Int -> Result DateTime DateTimeError
  • fromYmd : Int -> Int -> Int -> Result DateTime DateTimeError

Functions that return Maybe:

  • parse : String -> Maybe DateTime

Security

VariableEffect
HALYARD_DATETIME_DISABLED=1Disable all DateTime operations

Functions

Creation

DateTime.now

() ->{Clock} DateTime

Get the current UTC time.

Example:
halyard
import DateTime

DateTime.now ()

Notes: Takes Unit argument to trigger execution.

See also: DateTime.fromTimestamp, DateTime.format

DateTime.fromParts

Int -> Int -> Int -> Int -> Int -> Int -> Result DateTime DateTimeError

Create a DateTime from year, month, day, hour, minute, second.

Example:
halyard
import DateTime

case DateTime.fromParts 2024 1 15 10 30 0 of
    Ok dt -> DateTime.hour dt
    Err _ -> 0

Notes: Returns Err DateTimeError::InvalidDate for invalid component values (e.g., month 13, day 32).

See also: DateTime.fromYmd, DateTime.fromTimestamp

DateTime.fromYmd

Int -> Int -> Int -> Result DateTime DateTimeError

Create a DateTime from year, month, day at 00:00:00 UTC.

Example:
halyard
import DateTime

case DateTime.fromYmd 2024 1 15 of
    Ok dt -> DateTime.year dt
    Err _ -> 0

Notes: Returns Err DateTimeError::InvalidDate for invalid dates.

See also: DateTime.fromParts, DateTime.fromDate, DateTime.fromTimestamp

DateTime.fromDate

Date -> DateTime

Convert a Date to a DateTime at midnight UTC.

Example:
halyard
import Date
import DateTime

case Date.fromYmd 2024 6 15 of
    Ok date -> DateTime.fromDate date
    Err _ -> DateTime.fromTimestamp 0

Notes: Sets time to 00:00:00 UTC.

See also: DateTime.combine, DateTime.date

DateTime.fromTimestamp

Int -> DateTime

Create a DateTime from a Unix timestamp in seconds.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000

See also: DateTime.fromTimestampMillis, DateTime.toTimestamp

DateTime.fromTimestampMillis

Int -> DateTime

Create a DateTime from a Unix timestamp in milliseconds.

Example:
halyard
import DateTime

DateTime.fromTimestampMillis 1700000000000

See also: DateTime.fromTimestamp, DateTime.toTimestampMillis

DateTime.toTimestamp

DateTime -> Int

Convert a DateTime to a Unix timestamp in seconds.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.toTimestamp

See also: DateTime.toTimestampMillis, DateTime.fromTimestamp

DateTime.toTimestampMillis

DateTime -> Int

Convert a DateTime to a Unix timestamp in milliseconds.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.toTimestampMillis

See also: DateTime.toTimestamp, DateTime.fromTimestampMillis

Parsing

DateTime.parse

String -> Maybe DateTime

Parse an ISO8601 or RFC3339 datetime string (returns Maybe DateTime).

Example:
halyard
import DateTime

DateTime.parse "2024-01-15T10:30:00Z"

Notes: Accepts both ISO8601 and RFC3339 formats.

See also: DateTime.parseIso8601, DateTime.parseFormat

DateTime.parseIso8601

String -> Maybe DateTime

Parse an ISO8601 or RFC3339 datetime string (returns Maybe DateTime). RFC3339 is a strict subset of ISO8601, so this function accepts both formats.

Example:
halyard
import DateTime

DateTime.parseIso8601 "2024-01-15T10:30:00Z"

Notes: Accepts both ISO8601 and RFC3339 format strings.

See also: DateTime.parse, DateTime.parseFormat

DateTime.parseFormat

String -> String -> Maybe DateTime

Parse a datetime string using a custom strftime format (returns Maybe DateTime).

Example:
halyard
import DateTime

DateTime.parseFormat "%Y-%m-%d" "2024-01-15"

Notes: First argument is the format string, second is the datetime string.

See also: DateTime.parse, DateTime.formatCustom

Formatting

DateTime.format

DateTime -> String

Format a DateTime as an ISO8601 string.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.format

See also: DateTime.formatRfc3339, DateTime.formatCustom

DateTime.formatRfc3339

DateTime -> String

Format a DateTime as an RFC3339 string.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.formatRfc3339

See also: DateTime.format, DateTime.formatCustom

DateTime.formatCustom

String -> DateTime -> String

Format a DateTime using a custom strftime format.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.formatCustom "%Y-%m-%d"

Notes: Uses strftime format specifiers (e.g., %Y for year, %m for month, %d for day).

See also: DateTime.format, DateTime.parseFormat

Components

DateTime.year

DateTime -> Int

Get the year component (e.g., 2024).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.year

See also: DateTime.month, DateTime.day

DateTime.month

DateTime -> Int

Get the month component (1-12).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.month

See also: DateTime.year, DateTime.day

DateTime.day

DateTime -> Int

Get the day component (1-31).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.day

See also: DateTime.year, DateTime.month

DateTime.hour

DateTime -> Int

Get the hour component (0-23).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.hour

See also: DateTime.minute, DateTime.second

DateTime.minute

DateTime -> Int

Get the minute component (0-59).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.minute

See also: DateTime.hour, DateTime.second

DateTime.second

DateTime -> Int

Get the second component (0-59).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.second

See also: DateTime.hour, DateTime.minute

DateTime.weekday

DateTime -> Int

Get the weekday (1=Monday, 7=Sunday).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.weekday

Notes: Uses ISO weekday numbering: 1=Monday through 7=Sunday.

See also: DateTime.dayOfYear, DateTime.weekNumber

DateTime.dayOfYear

DateTime -> Int

Get the day of year (1-366).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.dayOfYear

See also: DateTime.weekday, DateTime.weekNumber

DateTime.weekNumber

DateTime -> Int

Get the ISO week number (1-53).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.weekNumber

See also: DateTime.weekday, DateTime.dayOfYear

Manipulation

DateTime.addMillis

Int -> DateTime -> DateTime

Add milliseconds to a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addMillis 5000

See also: DateTime.addSeconds, DateTime.diffMillis

DateTime.addSeconds

Int -> DateTime -> DateTime

Add seconds to a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addSeconds 60

See also: DateTime.addMinutes, DateTime.diffSeconds

DateTime.addMinutes

Int -> DateTime -> DateTime

Add minutes to a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addMinutes 30

See also: DateTime.addHours, DateTime.diffMinutes

DateTime.addHours

Int -> DateTime -> DateTime

Add hours to a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addHours 2

See also: DateTime.addDays, DateTime.diffHours

DateTime.addDays

Int -> DateTime -> DateTime

Add days to a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addDays 7

See also: DateTime.addWeeks, DateTime.diffDays

DateTime.addWeeks

Int -> DateTime -> DateTime

Add weeks to a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addWeeks 2

See also: DateTime.addDays, DateTime.addMonths

DateTime.addMonths

Int -> DateTime -> DateTime

Add months to a DateTime (handles month-end overflow).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addMonths 3

Notes: If the result day exceeds the target month's length, it clamps to the last day.

See also: DateTime.addYears, DateTime.addWeeks

DateTime.addYears

Int -> DateTime -> DateTime

Add years to a DateTime (handles leap year Feb 29).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.addYears 1

Notes: If the source date is Feb 29, adding a non-leap year clamps to Feb 28.

See also: DateTime.addMonths

Comparison

DateTime.isBefore

DateTime -> DateTime -> Bool

Check if the first DateTime is before the second.

Example:
halyard
import DateTime

DateTime.isBefore (DateTime.fromTimestamp 1000) (DateTime.fromTimestamp 2000)

See also: DateTime.isAfter, DateTime.isEqual, DateTime.compare

DateTime.isAfter

DateTime -> DateTime -> Bool

Check if the first DateTime is after the second.

Example:
halyard
import DateTime

DateTime.isAfter (DateTime.fromTimestamp 2000) (DateTime.fromTimestamp 1000)

See also: DateTime.isBefore, DateTime.isEqual, DateTime.compare

DateTime.isEqual

DateTime -> DateTime -> Bool

Check if two DateTimes are equal.

Example:
halyard
import DateTime

DateTime.isEqual (DateTime.fromTimestamp 1000) (DateTime.fromTimestamp 1000)

See also: DateTime.isBefore, DateTime.isAfter, DateTime.compare

DateTime.compare

DateTime -> DateTime -> Ordering

Compare two DateTimes, returning an Ordering value.

Example:
halyard
import DateTime
import Ordering

case DateTime.compare (DateTime.fromTimestamp 1000) (DateTime.fromTimestamp 2000) of
    Ordering::Lt -> "before"
    Ordering::Eq -> "same"
    Ordering::Gt -> "after"

See also: DateTime.isBefore, DateTime.isAfter, DateTime.isEqual

Duration

DateTime.diffMillis

DateTime -> DateTime -> Int

Calculate the difference in milliseconds between two DateTimes.

Example:
halyard
import DateTime

let dt1 = DateTime.fromTimestamp 1700000000

let dt2 = DateTime.fromTimestamp 1700000060

DateTime.diffMillis dt1 dt2

See also: DateTime.diffSeconds, DateTime.addMillis

DateTime.diffSeconds

DateTime -> DateTime -> Int

Calculate the difference in seconds between two DateTimes.

Example:
halyard
import DateTime

let dt1 = DateTime.fromTimestamp 1700000000

let dt2 = DateTime.fromTimestamp 1700000060

DateTime.diffSeconds dt1 dt2

See also: DateTime.diffMinutes, DateTime.addSeconds

DateTime.diffMinutes

DateTime -> DateTime -> Int

Calculate the difference in minutes between two DateTimes.

Example:
halyard
import DateTime

let dt1 = DateTime.fromTimestamp 1700000000

let dt2 = DateTime.fromTimestamp 1700003600

DateTime.diffMinutes dt1 dt2

See also: DateTime.diffHours, DateTime.addMinutes

DateTime.diffHours

DateTime -> DateTime -> Int

Calculate the difference in hours between two DateTimes.

Example:
halyard
import DateTime

let dt1 = DateTime.fromTimestamp 1700000000

let dt2 = DateTime.fromTimestamp 1700086400

DateTime.diffHours dt1 dt2

See also: DateTime.diffDays, DateTime.addHours

DateTime.diffDays

DateTime -> DateTime -> Int

Calculate the difference in days between two DateTimes.

Example:
halyard
import DateTime

let dt1 = DateTime.fromTimestamp 1700000000

let dt2 = DateTime.fromTimestamp 1700086400

DateTime.diffDays dt1 dt2

See also: DateTime.diffHours, DateTime.addDays

Calendar

DateTime.startOfDay

DateTime -> DateTime

Get the start of the day (00:00:00).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.startOfDay

See also: DateTime.endOfDay

DateTime.endOfDay

DateTime -> DateTime

Get the end of the day (23:59:59).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.endOfDay

See also: DateTime.startOfDay

DateTime.startOfWeek

DateTime -> DateTime

Get the start of the week (Monday 00:00:00).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.startOfWeek

Notes: Week starts on Monday (ISO 8601).

See also: DateTime.endOfWeek

DateTime.endOfWeek

DateTime -> DateTime

Get the end of the week (Sunday 23:59:59).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.endOfWeek

Notes: Week ends on Sunday (ISO 8601).

See also: DateTime.startOfWeek

DateTime.startOfMonth

DateTime -> DateTime

Get the start of the month (1st day 00:00:00).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.startOfMonth

See also: DateTime.endOfMonth

DateTime.endOfMonth

DateTime -> DateTime

Get the end of the month (last day 23:59:59).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.endOfMonth

See also: DateTime.startOfMonth

DateTime.startOfYear

DateTime -> DateTime

Get the start of the year (Jan 1 00:00:00).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.startOfYear

See also: DateTime.endOfYear

DateTime.endOfYear

DateTime -> DateTime

Get the end of the year (Dec 31 23:59:59).

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.endOfYear

See also: DateTime.startOfYear

Other

DateTime.combine

Date -> Time -> DateTime

Combine a Date and Time into a DateTime (UTC).

Example:
halyard
import Date
import Time
import DateTime

case Date.fromYmd 2024 6 15 of
    Ok date -> case Time.fromHms 14 30 0 of
        Ok time -> DateTime.combine date time
        Err _ -> DateTime.fromTimestamp 0
    Err _ -> DateTime.fromTimestamp 0

Notes: Creates a UTC DateTime from the given Date and Time.

See also: DateTime.fromDate, DateTime.date, DateTime.time

DateTime.date

DateTime -> Date

Extract the Date component from a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.date

See also: DateTime.time, DateTime.fromDate, DateTime.combine

DateTime.time

DateTime -> Time

Extract the Time component from a DateTime.

Example:
halyard
import DateTime

DateTime.fromTimestamp 1700000000
    |> DateTime.time

See also: DateTime.date, DateTime.fromDate, DateTime.combine