justinmimbs/date/3.0.0

NOTE: the latest version is 4.1.0

Date

type Date

Represents a date in an idealized calendar.

Month and Weekday types

The Month and Weekday types used in this package are aliases of Month and Weekday from elm/time. If you need to express literal values, like Jan or Mon, then you can install elm/time and import them from Time.

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


Date.fromCalendarDate 2020 Jan 1
Date.fromWeekDate 2020 1 Mon
type alias Month =
Month
type alias Weekday =
Weekday

Create

today : Task Never Date

Get the current local date. See this page for a full example.

fromPosix : Zone -> Posix -> Date

Create a date from a time Zone and a Posix time. This conversion loses the time information associated with the Posix value.

import Date exposing (fromCalendarDate, fromPosix)
import Time exposing (millisToPosix, utc, Month(..))


fromPosix utc (millisToPosix 0)
    == fromCalendarDate 1970 Jan 1
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.

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


fromCalendarDate 2018 Sep 26
fromWeekDate : Int -> Int -> Weekday -> Date

Create a date from an ISO week date: a week-numbering year, week number, and weekday. Out-of-range week number values will be clamped.

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


fromWeekDate 2018 39 Wed
fromOrdinalDate : Int -> Int -> Date

Create a date from an ordinal date: a year and day of the year. Out-of-range day values will be clamped.

import Date exposing (fromOrdinalDate)


fromOrdinalDate 2018 269
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.

-- calendar date
fromIsoString "2018-09-26"
    == Ok (fromCalendarDate 2018 Sep 26)




-- week date
fromIsoString "2018-W39-3"
    == Ok (fromWeekDate 2018 39 Wed)




-- ordinal date
fromIsoString "2018-269"
    == Ok (fromOrdinalDate 2018 269)

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 (2018, 2, 29)"
fromRataDie : Int -> Date

Rata Die is a system for assigning numbers to calendar days, where the number 1 represents the date 1 January 0001.

You can losslessly convert a Date to and from an Int representing the date in Rata Die. This makes it a convenient representation for transporting dates or using them as comparables. For all date values:

(date |> toRataDie |> fromRataDie)
    == date

Convert

toIsoString : Date -> String

Convert a date to a string in ISO 8601 extended format.

toIsoString (fromCalendarDate 2007 Mar 15)
    == "2007-03-15"
toRataDie : Date -> Int

Convert a date to its number representation in Rata Die (see fromRataDie). For all date values:

(date |> toRataDie |> fromRataDie)
    == date

Extract

year : Date -> Int

The calendar year.

month : Date -> Month

The month as a Month value (Jan–Dec).

day : Date -> Int

The day of the month (1–31).

weekYear : Date -> Int

The ISO week-numbering year. This is not always the same as the calendar year.

weekNumber : Date -> Int

The ISO week number of the year (1–53). Most week years have 52 weeks; some have 53.

weekday : Date -> Weekday

The weekday as a Weekday value (Mon–Sun).

ordinalDay : Date -> Int

The day of the year (1–366).

quarter : Date -> Int

The quarter of the year (1–4).

monthNumber : Date -> Int

The month number (1–12).

The weekday number (1–7), beginning with Monday.

Format

format : String -> Date -> String

Format a date using a string as a template.

format "EEEE, MMMM d, y" (fromCalendarDate 2007 Mar 15)
    == "Thursday, March 15, 2007"

Alphabetic characters in the template represent date information; the number of times a character is repeated specifies the form of a name (e.g. "Tue", "Tuesday") or the padding of a number (e.g. "1", "01").

Alphabetic characters can be escaped within single-quotes; a single-quote can be escaped as a sequence of two single-quotes, whether appearing inside or outside an escaped sequence.

Templates 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 (number or name)


"w" -- week number


"d" -- day


"D" -- ordinal day


"E" -- weekday name


"e" -- weekday number

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.

format "MMMM ddd, y" (fromCalendarDate 2007 Mar 15)
    == "March 15th, 2007"

Arithmetic

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

Get a past or future date by adding some number of units to a date.

add Weeks -2 (fromCalendarDate 2018 Sep 26)
    == fromCalendarDate 2018 Sep 12

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

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

Get 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.

ceiling Tuesday (fromCalendarDate 2018 May 11)
    == 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.

floor Tuesday (fromCalendarDate 2018 May 11)
    == 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.

start =
    fromCalendarDate 2018 May 8


until =
    fromCalendarDate 2018 May 14


range Day 2 start until
    == [ fromCalendarDate 2018 May 8
       , fromCalendarDate 2018 May 10
       , fromCalendarDate 2018 May 12
       ]

Month and Weekday helpers

monthToNumber : Month -> Int

Maps Jan–Dec to 1–12.

numberToMonth : Int -> Month

Maps 1–12 to Jan–Dec.

weekdayToNumber : Weekday -> Int

Maps Mon–Sun to 1-7.

numberToWeekday : Int -> Weekday

Maps 1-7 to Mon–Sun.