justinmimbs/elm-date-extra/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Date.Create

A Date represents a moment in time, encoded by two essential pieces of information: the number of milliseconds since 1 January 1970 UTC, and the offset between UTC and the current machine's local time. Extractions of a Date (e.g. Date.month, Date.hour) return representations of the date in local time, while Date.toTime returns the UTC representation.

Common Constructors

fromParts : Int -> Month -> Int -> Int -> Int -> Int -> Int -> Date

Create a Date from the following parts, given in local time:

  • year
  • month
  • day
  • hour
  • minute
  • second
  • millisecondimport Date exposing (Month(..)) import Date.Create as DateDate.fromParts 1999 Dec 31 23 59 0 0 -- <31 December 1999, 23:59, local time>

The values of the parts are not checked to ensure a valid date representation, nor are they clamped to valid range; instead, providing values outside a valid range results in underflow or overflow.

Date.fromParts 2007 Feb 29 0 0 0 0
-- <1 March 2007>
fromCalendarDate : Int -> Month -> Int -> Date

Convenience function for creating a Date from only the year, month, and day parts.

Date.fromCalendarDate 2000 Jan 1
fromIsoString : String -> Maybe Date

Attempt to create a Date from a string representing a date in ISO 8601 format.

Date.fromIsoString "2000-01-01"
-- Just <1 January 2000, local time>


Date.fromIsoString "2009-W01-1T00Z"
-- Just <29 December 2008, UTC>


Date.fromIsoString "2016-218T20:00-03:00"
-- Just <5 August 2016, 23:00, UTC>


Date.fromIsoString "1/1/2001"
-- Nothing

When a Date is created with a specified time zone offset (e.g. "-03:00"), its extractions still reflect the current machine's local time, and Date.toTime still reflects its UTC time.

Detailed Specification

In some cases you may want to specify a date with a time zone offset or from week-date or ordinal-date parts. The fromSpec function provides a way to do this.

fromSpec : TimeZone -> TimeSpec -> DateSpec -> Date

Create a Date from a specified time zone, time of day, and day.

Date.fromSpec
  local
  noTime
  (calendarDate 2000 Jan 1)
-- <1 January 2000, local time>


Date.fromSpec
  utc
  noTime
  (weekDate 2009 1 1)
-- <29 December 2008, UTC>


Date.fromSpec
  (offset -180)
  (atTime 20 0 0 0)
  (ordinalDate 2016 218)
-- <5 August 2016, 23:00, UTC>

When a Date is created with a specified time zone offset (e.g. offset -180), its extractions still reflect the current machine's local time, and Date.toTime still reflects its UTC time.

TimeZone

Represents a time zone.

utc : TimeZone

UTC time zone.

offset : Int -> TimeZone

Create a TimeZone with a specific offset from UTC time, given in minutes.

local : TimeZone

Do not specify a time zone, but use the local offset instead.

TimeSpec

Represents a time of day.

noTime : TimeSpec

Do not specify a time of day (default to 00:00).

atTime : Int -> Int -> Int -> Int -> TimeSpec

Create a TimeSpec from time parts (hour, minute, second, millisecond).

DateSpec

Represents a day.

calendarDate : Int -> Month -> Int -> DateSpec

Create a DateSpec from calendar-date parts (year, month, day).

weekDate : Int -> Int -> Int -> DateSpec

Create a DateSpec from week-date parts (weekYear, weekNumber, weekdayNumber).

ordinalDate : Int -> Int -> DateSpec

Create a DateSpec from ordinal-date parts (year, ordinalDay).

Julian Dates

fromJulianDate : Float -> Date

Create a Date from a Julian Date. Julian Dates always represent UTC time.

Date.fromJulianDate 2456293.52083333
-- <1 January 2013, 00:30, UTC>