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
import Date
Date.today ()Try itDate.fromYmd
Int -> Int -> Int -> Result Date DateError
Create a Date from year, month, day
import Date
Date.fromYmd 2024 6 15Try itDate.fromOrdinal
Int -> Int -> Result Date DateError
Create a Date from year and day of year (1-366)
import Date
Date.fromOrdinal 2024 100Try itParsing
Date.parse
String -> String -> Result Date DateError
Parse a date string using a strftime format
import Date
Date.parse "15/06/2024" "%d/%m/%Y"Try itDate.parseIso
String -> Result Date DateError
Parse an ISO 8601 date string (YYYY-MM-DD)
import Date
Date.parseIso "2024-06-15"Try itComponents
Date.year
Date -> Int
Get the year component
import Date
case Date.fromYmd 2024 6 15 of
Ok d -> Date.year d
Err _ -> 0Try itDate.month
Date -> Int
Get the month component (1-12)
import Date
case Date.fromYmd 2024 6 15 of
Ok d -> Date.month d
Err _ -> 0Try itDate.day
Date -> Int
Get the day component (1-31)
import Date
case Date.fromYmd 2024 6 15 of
Ok d -> Date.day d
Err _ -> 0Try itDate.weekday
Date -> Int
Get the weekday (1=Monday, 7=Sunday)
import Date
case Date.fromYmd 2024 1 1 of
Ok d -> Date.weekday d -- Monday = 1
Err _ -> 0Try itDate.dayOfYear
Date -> Int
Get the day of year (1-366)
import Date
case Date.fromYmd 2024 4 9 of
Ok d -> Date.dayOfYear d -- 100
Err _ -> 0Try itDate.weekNumber
Date -> Int
Get the ISO week number (1-53)
import Date
case Date.fromYmd 2024 1 1 of
Ok d -> Date.weekNumber d
Err _ -> 0Try itDate.isLeapYear
Date -> Bool
Check if the year is a leap year
import Date
case Date.fromYmd 2024 1 1 of
Ok d -> Date.isLeapYear d -- True
Err _ -> FalseTry itDate.daysInMonth
Date -> Int
Get the number of days in the month
import Date
case Date.fromYmd 2024 2 1 of
Ok d -> Date.daysInMonth d -- 29 (leap year)
Err _ -> 0Try itManipulation
Date.addDays
Int -> Date -> Date
Add days to a Date
import Date
case Date.fromYmd 2024 1 1 of
Ok d -> Date.toIsoString (Date.addDays 10 d)
Err _ -> "error"Try itDate.addWeeks
Int -> Date -> Date
Add weeks to a Date
import Date
case Date.fromYmd 2024 1 1 of
Ok d -> Date.toIsoString (Date.addWeeks 2 d)
Err _ -> "error"Try itDate.addMonths
Int -> Date -> Date
Add months to a Date (handles month-end overflow)
import Date
case Date.fromYmd 2024 1 31 of
Ok d -> Date.toIsoString (Date.addMonths 1 d) -- Feb 29
Err _ -> "error"Try itDate.addYears
Int -> Date -> Date
Add years to a Date (handles leap year Feb 29)
import Date
case Date.fromYmd 2024 2 29 of
Ok d -> Date.toIsoString (Date.addYears 1 d) -- Feb 28
Err _ -> "error"Try itDifference
Date.diffDays
Date -> Date -> Int
Calculate the difference in days between two dates
import Date
case (Date.fromYmd 2024 1 1, Date.fromYmd 2024 1 11) of
(Ok d1, Ok d2) -> Date.diffDays d1 d2 -- 10
_ -> 0Try itFormatting
Date.format
String -> Date -> String
Format a date using a strftime format string
import Date
case Date.fromYmd 2024 6 15 of
Ok d -> Date.format "%d/%m/%Y" d
Err _ -> "error"Try itDate.toIsoString
Date -> String
Format as ISO 8601 (YYYY-MM-DD)
import Date
case Date.fromYmd 2024 6 15 of
Ok d -> Date.toIsoString d -- "2024-06-15"
Err _ -> "error"Try itComparison
Date.isBefore
Date -> Date -> Bool
Check if the first date is before the second
import Date
case (Date.fromYmd 2024 1 1, Date.fromYmd 2024 12 31) of
(Ok d1, Ok d2) -> Date.isBefore d1 d2 -- True
_ -> FalseTry itDate.isAfter
Date -> Date -> Bool
Check if the first date is after the second
import Date
case (Date.fromYmd 2024 12 31, Date.fromYmd 2024 1 1) of
(Ok d1, Ok d2) -> Date.isAfter d1 d2 -- True
_ -> FalseTry itDate.isEqual
Date -> Date -> Bool
Check if two dates are equal
import Date
case (Date.fromYmd 2024 6 15, Date.fromYmd 2024 6 15) of
(Ok d1, Ok d2) -> Date.isEqual d1 d2 -- True
_ -> FalseTry itDate.compare
Date -> Date -> Int
Compare two dates (-1 if before, 0 if equal, 1 if after)
import Date
case (Date.fromYmd 2024 1 1, Date.fromYmd 2024 12 31) of
(Ok d1, Ok d2) -> Date.compare d1 d2 -- -1
_ -> 0Try it