tempo/date
Types
Functions
pub fn add(date: Date, days days: Int) -> Date
Adds a number of days to a date.
Examples
date.literal("2024-06-12")
|> date.add(days: 1)
// -> date.literal("2024-06-13")
date.literal("2024-06-12")
|> date.add(days: 12)
// -> date.literal("2024-06-24")
pub fn as_period(start: Date, end: Date) -> Period
Creates a period between two UTC dates at 00:00:00 each.
Examples
date.literal("2024-06-12")
|> date.difference(from: date.literal("2024-06-23"))
|> period.as_days
// -> 11
date.literal("2024-06-12")
|> date.difference(from: date.literal("2024-06-03"))
|> period.as_days
// -> 9
pub fn compare(a: Date, to b: Date) -> Order
Compares two dates.
Examples
date.literal("2024-06-12")
|> date.compare(to: date.literal("2024-06-12"))
// -> order.Eq
date.literal("2024-05-12")
|> date.compare(to: date.literal("2024-06-13"))
// -> order.Lt
date.literal("2034-06-12")
|> date.compare(to: date.literal("2024-06-11"))
// -> order.Gt
pub fn current_local() -> Date
Gets the current local date of the host.
Examples
date.current_local()
|> date.to_string
// -> "2024-06-13"
pub fn current_utc() -> Date
Gets the current UTC date of the host.
Examples
date.current_utc()
|> date.to_string
// -> "2024-06-14"
pub fn difference(of a: Date, from b: Date) -> Period
Gets the difference between two dates as a period between the two UTC dates at 00:00:00 each.
Examples
date.literal("2024-06-12")
|> date.difference(from: date.literal("2024-06-23"))
|> period.as_days
// -> 11
date.literal("2024-06-12")
|> date.difference(from: date.literal("2024-06-03"))
|> period.as_days
// -> 9
pub fn from_string(date: String) -> Result(Date, Error)
Parses a date string in the format YYYY-MM-DD
, YYYY-M-D
, YYYY/MM/DD
,
YYYY/M/D
, YYYY.MM.DD
, YYYY.M.D
, YYYY_MM_DD
, YYYY_M_D
, YYYY MM DD
,
YYYY M D
, or YYYYMMDD
.
Examples
date.from_string("2024-06-13")
// -> Ok(date.literal("2024-06-13"))
date.from_string("20240613")
// -> Ok(date.literal("2024-06-13"))
date.from_string("2409")
// -> Error(tempo.DateInvalidFormat)
pub fn from_tuple(date: #(Int, Int, Int)) -> Result(Date, Error)
Returns a date value from a tuple of ints if the values represent the years, month, and day of a valid date. The year must be greater than 1000.
Years less than 1000 are technically valid years, but are not common and usually indicate that either a non-year value was passed as the year or a two digit year was passed (which are too abiguous to be confidently accepted).
Examples
date.from_tuple(#(2024, 6, 13))
// -> Ok(date.literal("2024-06-13"))
date.from_tuple(#(98, 6, 13))
// -> Error(tempo.DateOutOfBounds)
pub fn get_day(date: Date) -> Int
Gets the day value of a date.
Examples
date.literal("2024-06-13")
|> date.get_day
// -> 13
pub fn get_month(date: Date) -> Month
Gets the month value of a date.
Examples
date.literal("2024-06-13")
|> date.get_month
// -> tempo.Jun
pub fn get_year(date: Date) -> Int
Gets the year value of a date.
Examples
date.literal("2024-06-13")
|> date.get_year
// -> 2024
pub fn is_earlier(a: Date, than b: Date) -> Bool
Checks of the first date is earlier than the second date.
Examples
date.literal("2024-06-12")
|> date.is_earlier(than: date.literal("2024-06-13"))
// -> True
date.literal("2024-06-12")
|> date.is_earlier(than: date.literal("2024-06-12"))
// -> False
pub fn is_earlier_or_equal(a: Date, to b: Date) -> Bool
Checks if the first date is earlier than or equal to the second date.
Examples
date.literal("2024-06-12")
|> date.is_earlier_or_equal(to: date.literal("2024-06-12"))
// -> True
date.literal("2024-06-12")
|> date.is_earlier_or_equal(to: date.literal("2024-06-11"))
// -> False
pub fn is_equal(a: Date, to b: Date) -> Bool
Checks if two dates are equal.
Example
date.literal("2024-06-12")
|> date.is_equal(to: date.literal("2024-06-12"))
// -> True
pub fn is_later(a: Date, than b: Date) -> Bool
Checks if the first date is later than the second date.
Examples
date.literal("2024-06-14")
|> date.is_later(than: date.literal("2024-06-13"))
// -> True
date.literal("2024-06-12")
|> date.is_later(than: date.literal("2024-06-12"))
// -> False
pub fn is_later_or_equal(a: Date, to b: Date) -> Bool
Checks if the first date is later than or equal to the second date.
Examples
date.literal("2024-06-12")
|> date.is_later_or_equal(to: date.literal("2024-06-12"))
// -> True
date.literal("2024-06-12")
|> date.is_later_or_equal(to: date.literal("2024-06-13"))
// -> False
pub fn is_weekend(date: Date) -> Bool
Checks if a date falls in a weekend.
Examples
date.literal("2024-06-22")
|> date.is_weekend
// -> True
pub fn literal(date: String) -> Date
Creates a new date value from a string literal, but will panic if
the string is invalid. Accepted formats are YYYY-MM-DD
, YYYY-M-D
,
YYYY/MM/DD
, YYYY/M/D
, YYYY.MM.DD
, YYYY.M.D
, YYYY_MM_DD
,
YYYY_M_D
, YYYY MM DD
, YYYY M D
, or YYYYMMDD
.
Useful for declaring date literals that you know are valid within your
program.
Examples
date.literal("2024-06-13")
|> date.to_string
// -> "2024-06-13"
date.literal("20240613")
|> date.to_string
// -> "2024-06-13"
date.literal("2409")
// -> panic
pub fn new(
year year: Int,
month month: Int,
day day: Int,
) -> Result(Date, Error)
Creates a new date and validates it.
Examples
date.new(2024, 6, 13)
// -> Ok(date.literal("2024-06-13"))
date.new(2024, 6, 31)
// -> Error(tempo.DateOutOfBounds)
pub fn subtract(date: Date, days days: Int) -> Date
Subtracts a number of days from a date.
Examples
date.literal("2024-06-12")
|> date.subtract(days: 1)
// -> date.literal("2024-06-11")
date.literal("2024-06-12")
|> date.subtract(days: 12)
// -> date.literal("2024-05-31")
pub fn to_string(date: Date) -> String
Returns a string representation of a date value in the format YYYY-MM-DD
.
Examples
date.literal("2024-06-13")
|> date.to_string
// -> "2024-06-13"
pub fn to_tuple(date: Date) -> #(Int, Int, Int)
Returns a tuple of ints from a date value that represent the year, month, and day of the date.
Examples
date.literal("2024-06-14")
|> date.to_tuple
// -> #(2024, 6, 14)
pub fn to_weekday(date: Date) -> DayOfWeek
Returns the day of week a date falls on. Will be incorrect for dates before 1752 and dates after 2300.
Examples
date.literal("2024-06-20")
|> date.to_weekday
// -> Thur