Esc
Start typing to search...

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

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

() -> Date

Get the current local date

Example:
import Date

Date.today ()
Try it

Date.fromYmd

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

Create a Date from year, month, day

Example:
import Date

Date.fromYmd 2024 6 15
Try it

Date.fromOrdinal

Int -> Int -> Result Date DateError

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

Example:
import Date

Date.fromOrdinal 2024 100
Try it

Parsing

Date.parse

String -> String -> Result Date DateError

Parse a date string using a strftime format

Example:
import Date

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

Date.parseIso

String -> Result Date DateError

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

Example:
import Date

Date.parseIso "2024-06-15"
Try it

Components

Date.year

Date -> Int

Get the year component

Example:
import Date

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

Date.month

Date -> Int

Get the month component (1-12)

Example:
import Date

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

Date.day

Date -> Int

Get the day component (1-31)

Example:
import Date

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

Date.weekday

Date -> Int

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

Example:
import Date

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

Date.dayOfYear

Date -> Int

Get the day of year (1-366)

Example:
import Date

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

Date.weekNumber

Date -> Int

Get the ISO week number (1-53)

Example:
import Date

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

Date.isLeapYear

Date -> Bool

Check if the year is a leap year

Example:
import Date

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

Date.daysInMonth

Date -> Int

Get the number of days in the month

Example:
import Date

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

Manipulation

Date.addDays

Int -> Date -> Date

Add days to a Date

Example:
import Date

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

Date.addWeeks

Int -> Date -> Date

Add weeks to a Date

Example:
import Date

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

Date.addMonths

Int -> Date -> Date

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

Example:
import Date

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

Date.addYears

Int -> Date -> Date

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

Example:
import Date

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

Difference

Date.diffDays

Date -> Date -> Int

Calculate the difference in days between two dates

Example:
import Date

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

Formatting

Date.format

String -> Date -> String

Format a date using a strftime format string

Example:
import Date

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

Date.toIsoString

Date -> String

Format as ISO 8601 (YYYY-MM-DD)

Example:
import Date

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

Comparison

Date.isBefore

Date -> Date -> Bool

Check if the first date is before the second

Example:
import Date

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

Date.isAfter

Date -> Date -> Bool

Check if the first date is after the second

Example:
import Date

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

Date.isEqual

Date -> Date -> Bool

Check if two dates are equal

Example:
import Date

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

Date.compare

Date -> Date -> Int

Compare two dates (-1 if before, 0 if equal, 1 if after)

Example:
import Date

case (Date.fromYmd 2024 1 1, Date.fromYmd 2024 12 31) of
    (Ok d1, Ok d2) -> Date.compare d1 d2  -- -1
    _ -> 0
Try it