elm-community/elm-time/1.0.6

NOTE: this package is not compatible with Elm 0.19.1

Time.Date

This module defines a timezone-independent Date type which can represent any date of the proleptic Gregorian calendar.

Dates

type Date

Date is the opaque type for all Date values. Values of this type are guaranteed to represent valid proleptic Gregorian calendar dates.

date : Int -> Int -> Int -> Date

date constructs a Date value given a year, a month and a day. Invalid values are clamped to the nearest valid date.

year : Date -> Int

year returns a Date's year as an Int.

month : Date -> Int

month returns a Date's month as an Int. Guaranteed to be in the range [1, 12].

day : Date -> Int

day returns a Date's year as an Int. Guaranteed to be valid for the Date's (year, month) pair and in the range [1, 31].

type Weekday
= Mon
| Tue
| Wed
| Thu
| Fri
| Sat
| Sun

Data type used to represent the days of the week.

weekday : Date -> Weekday

weekday returns the day of week for a given Date.

This uses Sakamoto's method to determine the day of week.

Manipulating Dates

setYear : Int -> Date -> Date

setYear updates a Date's year. Invalid values are clamped to the nearest valid date.

setMonth : Int -> Date -> Date

setMonth updates a Date's month. Invalid values are clamped to the nearest valid date.

setDay : Int -> Date -> Date

setDay updates a Date's day. Invalid values are clamped to the nearest valid date.

addYears : Int -> Date -> Date

addYears adds a relative number (positive or negative) of years to a Date, ensuring that the return value represents a valid Date. If the new date is not valid, days are subtracted from it until a valid Date can be produced.

addMonths : Int -> Date -> Date

addMonths adds a relative number (positive or negative) of months to a Date, ensuring that the return value represents a valid Date. Its semantics are the same as addYears.

addDays : Int -> Date -> Date

days adds an exact number (positive or negative) of days to a Date. Adding or subtracting days always produces a valid Date so there is no fuzzing logic here like there is in add{Months,Years}.

Comparing Dates

compare : Date -> Date -> Order

compare two Dates.

Subtracting Dates

type alias DateDelta =
{ years : Int
, months : Int
, days : Int
}

DateDelta represents a delta between two dates.

delta : Date -> Date -> DateDelta

delta returns the relative number of years, months and days between two Dates.

Helper functions

toISO8601 : Date -> String

toISO8601 converts a Date into its string representation.

fromISO8601 : String -> Result String Date

fromISO8601 parses an ISO8601-formatted date string into a Date.

toTuple : Date -> ( Int, Int, Int )

toTuple converts a Date value into a (year, month, day) tuple. This is useful if you want to use Dates as Dict keys.

fromTuple : ( Int, Int, Int ) -> Date

fromTuple converts a (year, month, day) tuple into a Date value.

isValidDate : Int -> Int -> Int -> Bool

isValidDate returns True if the given year, month and day represent a valid date.

isLeapYear : Int -> Bool

isLeapYear returns True if the given year is a leap year. The rules for leap years are as follows:

  • A year that is a multiple of 400 is a leap year.
  • A year that is a multiple of 100 but not of 400 is not a leap year.
  • A year that is a multiple of 4 but not of 100 is a leap year.
daysInMonth : Int -> Int -> Maybe Int

daysInMonth returns the number of days in a month given a specific year, taking leap years into account.

  • A regular year has 365 days and the corresponding February has 28 days.
  • A leap year has 366 days and the corresponding February has 29 days.