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

Search

Menu

Date Module

Functions for working with dates (without time) backed by chrono.

The Date module provides a Date type for representing calendar dates. Unlike DateTime which uses Maybe for fallible operations, Date uses Result with typed DateError variants for precise error handling.

Common patterns

halyard
import Date

-- Create from components
Date.fromYmd 2024 6 15

-- Parse and manipulate
Date.parseIso "2024-01-15"
    |> Result.map (Date.addDays 30)

Return types

Functions that can fail return Result Date DateError.

Functions

Creation

Date.today

() ->{Clock} Date

Get the current local date

Example:
halyard
import Date

Date.today ()

Date.fromYmd

Int -> Int -> Int -> Result Date DateError

Create a Date from year, month, day

Example:
halyard
import Date

Date.fromYmd 2024 6 15

Date.fromOrdinal

Int -> Int -> Result Date DateError

Create a Date from year and day of year (1-366)

Example:
halyard
import Date

Date.fromOrdinal 2024 100

Parsing

Date.parse

String -> String -> Result Date DateError

Parse a date string using a strftime format

Example:
halyard
import Date

Date.parse "15/06/2024" "%d/%m/%Y"

Date.parseIso

String -> Result Date DateError

Parse an ISO 8601 date string (YYYY-MM-DD)

Example:
halyard
import Date

Date.parseIso "2024-06-15"

Components

Date.year

Date -> Int

Get the year component

Example:
halyard
import Date

case Date.fromYmd 2024 6 15 of
    Ok d -> Date.year d
    Err _ -> 0

Date.month

Date -> Int

Get the month component (1-12)

Example:
halyard
import Date

case Date.fromYmd 2024 6 15 of
    Ok d -> Date.month d
    Err _ -> 0

Date.day

Date -> Int

Get the day component (1-31)

Example:
halyard
import Date

case Date.fromYmd 2024 6 15 of
    Ok d -> Date.day d
    Err _ -> 0

Date.weekday

Date -> Int

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

Example:
halyard
import Date

case Date.fromYmd 2024 1 1 of
    Ok d -> Date.weekday d  -- Monday = 1
    Err _ -> 0

Date.dayOfYear

Date -> Int

Get the day of year (1-366)

Example:
halyard
import Date

case Date.fromYmd 2024 4 9 of
    Ok d -> Date.dayOfYear d  -- 100
    Err _ -> 0

Date.weekNumber

Date -> Int

Get the ISO week number (1-53)

Example:
halyard
import Date

case Date.fromYmd 2024 1 1 of
    Ok d -> Date.weekNumber d
    Err _ -> 0

Date.isLeapYear

Date -> Bool

Check if the year is a leap year

Example:
halyard
import Date

case Date.fromYmd 2024 1 1 of
    Ok d -> Date.isLeapYear d  -- True
    Err _ -> False

Date.daysInMonth

Date -> Int

Get the number of days in the month

Example:
halyard
import Date

case Date.fromYmd 2024 2 1 of
    Ok d -> Date.daysInMonth d  -- 29 (leap year)
    Err _ -> 0

Manipulation

Date.addDays

Int -> Date -> Date

Add days to a Date

Example:
halyard
import Date

case Date.fromYmd 2024 1 1 of
    Ok d -> Date.toIsoString (Date.addDays 10 d)
    Err _ -> "error"

Date.addWeeks

Int -> Date -> Date

Add weeks to a Date

Example:
halyard
import Date

case Date.fromYmd 2024 1 1 of
    Ok d -> Date.toIsoString (Date.addWeeks 2 d)
    Err _ -> "error"

Date.addMonths

Int -> Date -> Date

Add months to a Date (handles month-end overflow)

Example:
halyard
import Date

case Date.fromYmd 2024 1 31 of
    Ok d -> Date.toIsoString (Date.addMonths 1 d)  -- Feb 29
    Err _ -> "error"

Date.addYears

Int -> Date -> Date

Add years to a Date (handles leap year Feb 29)

Example:
halyard
import Date

case Date.fromYmd 2024 2 29 of
    Ok d -> Date.toIsoString (Date.addYears 1 d)  -- Feb 28
    Err _ -> "error"

Difference

Date.diffDays

Date -> Date -> Int

Calculate the difference in days between two dates

Example:
halyard
import Date

case (Date.fromYmd 2024 1 1, Date.fromYmd 2024 1 11) of
    (Ok d1, Ok d2) -> Date.diffDays d1 d2  -- 10
    _ -> 0

Formatting

Date.format

String -> Date -> String

Format a date using a strftime format string

Example:
halyard
import Date

case Date.fromYmd 2024 6 15 of
    Ok d -> Date.format "%d/%m/%Y" d
    Err _ -> "error"

Date.toIsoString

Date -> String

Format as ISO 8601 (YYYY-MM-DD)

Example:
halyard
import Date

case Date.fromYmd 2024 6 15 of
    Ok d -> Date.toIsoString d  -- "2024-06-15"
    Err _ -> "error"

Comparison

Date.isBefore

Date -> Date -> Bool

Check if the first date is before the second

Example:
halyard
import Date

case (Date.fromYmd 2024 1 1, Date.fromYmd 2024 12 31) of
    (Ok d1, Ok d2) -> Date.isBefore d1 d2  -- True
    _ -> False

Date.isAfter

Date -> Date -> Bool

Check if the first date is after the second

Example:
halyard
import Date

case (Date.fromYmd 2024 12 31, Date.fromYmd 2024 1 1) of
    (Ok d1, Ok d2) -> Date.isAfter d1 d2  -- True
    _ -> False

Date.isEqual

Date -> Date -> Bool

Check if two dates are equal

Example:
halyard
import Date

case (Date.fromYmd 2024 6 15, Date.fromYmd 2024 6 15) of
    (Ok d1, Ok d2) -> Date.isEqual d1 d2  -- True
    _ -> False

Date.compare

Date -> Date -> Ordering

Compare two dates, returning an Ordering value

Example:
halyard
import Date
import Ordering

case (Date.fromYmd 2024 1 1, Date.fromYmd 2024 12 31) of
    (Ok d1, Ok d2) -> case Date.compare d1 d2 of
        Ordering::Lt -> "before"
        Ordering::Eq -> "same"
        Ordering::Gt -> "after"
    _ -> "error"