justinmimbs/date/2.0.0

NOTE: the latest version is 4.1.0

Date.RataDie

Rata Die is a system for assigning numbers to calendar days, using a base date of 1 January 0001.

This module exposes the same functions as the Date module, but it uses raw Int values as date representations.

This may be useful if you need dates as comparables. Otherwise, the Date module offers an opaque type for better type-safety.

type alias RataDie =
Int
type alias Month =
Month

The Month type used in this package is an alias of Time.Month from elm/time. So if you need to express Month values, like 'Jan', then you'll need to import them from Time.

import Date exposing (Date)
import Time exposing (Month(..))


Date.fromCalendarDate 2020 Jan 1
type alias Weekday =
Weekday

The Weekday type used in this package is an alias of Time.Weekday from elm/time. So if you need to express Weekday values, like 'Mon', then you'll need to import them from Time.

import Date exposing (Date)
import Time exposing (Weekday(..))


Date.fromWeekDate 2020 1 Mon

Dates

fromCalendarDate : Int -> Month -> Int -> RataDie

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 -> RataDie

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 -> RataDie

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 RataDie

Get the current local date.

Formatting

format : String -> RataDie -> String

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

fromCalendarDate 2007 Mar 15
    |> format "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
    |> format "MMMM ddd, y"
-- "March 15th, 2007"

ISO 8601

fromIsoString : String -> Result String RataDie

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 : RataDie -> 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 -> RataDie -> RataDie

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 -> RataDie -> RataDie -> 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
ceiling : Interval -> RataDie -> RataDie

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
floor : Interval -> RataDie -> RataDie

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 -> RataDie -> RataDie -> List RataDie

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 : RataDie -> Int
quarter : RataDie -> Int
month : RataDie -> Month
monthNumber : RataDie -> Int
ordinalDay : RataDie -> Int

Extracts the day of the year.

day : RataDie -> Int

Extracts the day of the month.

weekYear : RataDie -> Int

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

weekNumber : RataDie -> Int
weekday : RataDie -> Weekday
weekdayNumber : RataDie -> Int

Numbers 1–7 represent Monday–Sunday.

Month and Weekday helpers

monthToNumber : Month -> Int
numberToMonth : Int -> Month
weekdayToNumber : Weekday -> Int
numberToWeekday : Int -> Weekday