justinmimbs/date/1.1.0

NOTE: the latest version is 4.1.0

Date

type Date

Represents a date without a time or zone.

type Month
= Jan
| Feb
| Mar
| Apr
| May
| Jun
| Jul
| Aug
| Sep
| Oct
| Nov
| Dec
type Weekday
= Mon
| Tue
| Wed
| Thu
| Fri
| Sat
| Sun

Constructors

fromCalendarDate : Int -> Month -> Int -> Date

Create a date from a year, month, and day of the month; out-of-range day values will be clamped.

fromCalendarDate 2018 Sep 26
fromOrdinalDate : Int -> Int -> Date

Create a date from a year and day of the year; out-of-range day values will be clamped.

fromOrdinalDate 2018 314
fromWeekDate : Int -> Int -> Weekday -> Date

Create a date from a week-numbering year, week number, and weekday; out-of-range week values will be clamped.

fromWeekDate 2018 26 Wed
today : Task Never Date

Get the current local date.

Formatting

toFormattedString : String -> Date -> String

Convert a date to a string using a pattern as a template.

fromCalendarDate 2007 Mar 15
    |> toFormattedString "EEEE, MMMM d, y"
-- "Thursday, March 15, 2007"

Each alphabetic character in the pattern represents date or time information; the number of times a character is repeated specifies the form of the name to use (e.g. "Tue", "Tuesday") or the padding of numbers (e.g. "1", "01"). Formatting characters are escaped within single-quotes; a single-quote is escaped as a sequence of two single-quotes, whether appearing inside or outside an escaped sequence.

Patterns are based on Date Format Patterns in Unicode Technical Standard #35. Only the following subset of formatting characters are available:

"y" -- year
"Y" -- week-numbering year
"Q" -- quarter
"M" -- month
"w" -- week number
"d" -- day
"D" -- ordinal day
"E" -- day of week
"e" -- weekday number / day of week

The non-standard pattern field "ddd" is available to indicate the day of the month with an ordinal suffix (e.g. "1st", "15th"), as the current standard does not include such a field.

fromCalendarDate 2007 Mar 15
    |> toFormattedString "MMMM ddd, y"
-- "March 15th, 2007"

ISO 8601

fromIsoString : String -> Result String Date

Attempt to create a date from a string in ISO 8601 format. Calendar dates, week dates, and ordinal dates are all supported in extended and basic format.

fromIsoString "2018-09-26"
fromIsoString "2018-W26-3"
fromIsoString "2018-314"

The string must represent a valid date; unlike fromCalendarDate and friends, any out-of-range values will fail to produce a date.

fromIsoString "2018-02-29"
-- Err "Invalid calendar date"
toIsoString : Date -> String

Convenience function for formatting a date in ISO 8601 extended format.

fromCalendarDate 2007 Mar 15
    |> toIsoString
-- "2007-03-15"

Arithmetic

type Unit
= Years
| Months
| Weeks
| Days
add : Unit -> Int -> Date -> Date

Move a date by some number of units.

fromCalendarDate 2018 Sep 26
    |> add Weeks -2
-- fromCalendarDate 2018 Sep 12

When adding Years or Months, day values are clamped to the end of the month if necessary.

fromCalendarDate 2000 Jan 31
    |> add Months 1
-- fromCalendarDate 2000 Feb 29
diff : Unit -> Date -> Date -> Int

Find the difference, as a number of some units, between two dates.

diff Months
    (fromCalendarDate 2007 Mar 15)
    (fromCalendarDate 2007 Sep 1)
-- 5

Rounding

= Year
| Quarter
| Month
| Week
| Monday
| Tuesday
| Wednesday
| Thursday
| Friday
| Saturday
| Sunday
| Day

Round up a date to the beginning of the closest interval. The resulting date will be greater than or equal to the one provided.

fromCalendarDate 2018 May 11
    |> ceiling Tuesday
-- fromCalendarDate 2018 May 15

Round down a date to the beginning of the closest interval. The resulting date will be less than or equal to the one provided.

fromCalendarDate 2018 May 11
    |> floor Tuesday
-- fromCalendarDate 2018 May 8

Lists

range : Interval -> Int -> Date -> Date -> List Date

Create a list of dates, at rounded intervals, increasing by a step value, between two dates. The list will start on or after the first date, and end before the second date.

range Day 2
    (fromCalendarDate 2018 May 8)
    (fromCalendarDate 2018 May 14)


-- [ fromCalendarDate 2018 May 8
-- , fromCalendarDate 2018 May 10
-- , fromCalendarDate 2018 May 12
-- ]

Extractions

year : Date -> Int
quarter : Date -> Int
monthNumber : Date -> Int
ordinalDay : Date -> Int

Extracts the day of the year.

day : Date -> Int

Extracts the day of the month.

weekYear : Date -> Int

Extracts the week-numbering year; this is not always the same as the calendar year.

weekNumber : Date -> Int

Numbers 1–7 represent Monday–Sunday.

Records

Convenience functions for converting dates to records.

-> { year : Int
, month : Month
, day : Int
}
-> { year : Int
, ordinalDay : Int
}
-> { weekYear : Int
, weekNumber : Int
, weekday : Weekday
}

Month and Weekday helpers

Rata Die

Convert a Date to and from a raw Int representing the date in Rata Die.

toRataDie : Date -> Int
fromRataDie : Int -> Date