elm-community/elm-time

Name: elm-time

Owner: Elm Community

Description: A pure Elm date and time library.

Created: 2016-09-26 12:08:00.0

Updated: 2018-05-24 18:23:05.0

Pushed: 2018-05-24 04:50:28.0

Homepage: http://package.elm-lang.org/packages/elm-community/elm-time/latest

Size: 244

Language: Elm

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

elm-time Build Status

package install elm-community/elm-time
Dates

Dates may represent any date in the proleptic Gregorian calendar.

rt Time.Date as Date exposing (Date, date)
Constructing Dates

Use date to construct Date values. If given invalid values for the month and day, they are both clamped and the nearest valid date is returned.

te 1992 2 28
 { year = 1992, month = 2, day = 28 } : Date

te 1992 2 31
 { year = 1992, month = 2, day = 29 } : Date

te 1992 2 128
 { year = 1992, month = 2, day = 29 } : Date

Use year, month, and day to inspect Dates.

rthday = date 1992 5 29
 { year = 1992, month = 5, day = 29 } : Date

te.year birthday
 : Int

te.month birthday
Int

te.day birthday
 Int
Manipulating Dates

setYear, setMonth and setDay can be used to create new Dates containing updated values for each respective field. Like date, these functions clamp their parameters and return the nearest valid date.

addDays can be used to add an exact number of days to a Date.

addYears and addMonths add a relative number of years and months to a date. If the target date is invalid, these functions continually subtract one day until a valid date is found.

te 1992 1 31
|> Date.addYears 1
|> Date.toISO8601
3-01-31" : String

te 1992 2 29
|> Date.addYears 1
|> Date.toISO8601
3-02-28" : String

te 1992 1 31
|> Date.addMonths 1
|> Date.toISO8601
2-02-28" : String
DateTimes

DateTimes represent a Date together with a time offset from midnight.

rt Time.DateTime as DateTime exposing (DateTime, dateTime)
Constructing DateTimes

DateTimes can be constructed from a record using the dateTime function or from a UTC timestamp in milliseconds using fromTimestamp. To construct a DateTime using dateTime, pass it a record containing fields for year, month, day, hour, minute, second and millisecond:

teTime { year = 1992, month = 5, day = 29, hour = 0, minute = 0, second = 0, millisecond = 0 }
Time { date = Date { year = 1992, month = 5, day = 29 }, offset = 0 } : Date

teTime { year = 1992, month = 2, day = 31, hour = 0, minute = 0, second = 0, millisecond = 0 }
Time { date = Date { year = 1992, month = 2, day = 29 }, offset = 0 } : Date

To make constructing DateTimes less tedious, the library provides Time.DateTime.zero:

port Time.DateTime as DateTime exposing (DateTime, dateTime, zero)

teTime { zero | year = 1992 }
|> DateTime.toISO8601
2-01-01T00:00:00.000Z" : String

teTime { zero | year = 1992, month = 2, day = 28, hour = 5 }
|> DateTime.toISO8601
2-02-28T05:00:00.000Z" : String

Use fromTimestamp to construct a DateTime from a UTC timestamp in milliseconds:

omTimestamp 0
|> DateTime.toISO8601
0-01-01T00:00:00.000Z" : String

See examples/without-timezone for an example of how to construct DateTimes from local time.

Manipulating DateTimes

Like Time.Date, the DateTime module exposes functions for adding to and updating a DateTime's fields. The functions addYears and addMonths have the same behaviour as their Time.Date counterparts.

ZonedDateTimes

ZonedDateTimes represent a DateTime in a specific TimeZone. See examples/with-timezone for an example of how to use ZonedDateTimes.

rt Time.TimeZones as TimeZones
rt Time.ZonedDateTime as ZonedDateTime exposing (ZonedDateTime)

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.