Skip to content

datetime

import datetime
FunctionSignatureDescription
now() -> IntGet the current time as a Unix timestamp (seconds, UTC).
from_parts(Int, Int, Int, Int, Int, Int) -> IntCreate a timestamp from year, month, day, hour, minute, second (UTC).
parse_iso(String) -> Result[Int, String]Parse an ISO 8601 date string into a timestamp.
from_unix(Int) -> IntConvert a Unix timestamp (identity function for documentation clarity).
format(Int, String) -> StringFormat a timestamp using a pattern string.
to_iso(Int) -> StringFormat a timestamp as ISO 8601 string.
to_unix(Int) -> IntGet the Unix timestamp value (identity function).
year(Int) -> IntExtract the year from a timestamp.
month(Int) -> IntExtract the month (1-12) from a timestamp.
day(Int) -> IntExtract the day of month (1-31) from a timestamp.
hour(Int) -> IntExtract the hour (0-23) from a timestamp.
minute(Int) -> IntExtract the minute (0-59) from a timestamp.
second(Int) -> IntExtract the second (0-59) from a timestamp.
weekday(Int) -> StringGet the day of week as a string (Monday-Sunday).
add_days(Int, Int) -> IntAdd n days to a timestamp.
add_hours(Int, Int) -> IntAdd n hours to a timestamp.
add_minutes(Int, Int) -> IntAdd n minutes to a timestamp.
add_seconds(Int, Int) -> IntAdd n seconds to a timestamp.
diff_seconds(Int, Int) -> IntCompute the difference in seconds between two timestamps.
is_before(Int, Int) -> BoolCheck if timestamp a is before timestamp b.
is_after(Int, Int) -> BoolCheck if timestamp a is after timestamp b.

Get the current time as a Unix timestamp (seconds, UTC).

let ts = datetime.now()

datetime.from_parts(y: Int, m: Int, d: Int, h: Int, min: Int, s: Int) -> Int

Section titled “datetime.from_parts(y: Int, m: Int, d: Int, h: Int, min: Int, s: Int) -> Int”

Create a timestamp from year, month, day, hour, minute, second (UTC).

datetime.from_parts(2024, 1, 15, 12, 0, 0)

datetime.parse_iso(s: String) -> Result[Int, String]

Section titled “datetime.parse_iso(s: String) -> Result[Int, String]”

Parse an ISO 8601 date string into a timestamp.

datetime.parse_iso("2024-01-15T12:00:00Z") // => ok(1705320000)

Convert a Unix timestamp (identity function for documentation clarity).

datetime.from_unix(1705320000)

datetime.format(ts: Int, pattern: String) -> String

Section titled “datetime.format(ts: Int, pattern: String) -> String”

Format a timestamp using a pattern string.

datetime.format(ts, "%Y-%m-%d") // => "2024-01-15"

Format a timestamp as ISO 8601 string.

datetime.to_iso(1705320000) // => "2024-01-15T12:00:00Z"

Get the Unix timestamp value (identity function).

datetime.to_unix(ts) // => 1705320000

Extract the year from a timestamp.

datetime.year(ts) // => 2024

Extract the month (1-12) from a timestamp.

datetime.month(ts) // => 1

Extract the day of month (1-31) from a timestamp.

datetime.day(ts) // => 15

Extract the hour (0-23) from a timestamp.

datetime.hour(ts) // => 12

Extract the minute (0-59) from a timestamp.

datetime.minute(ts) // => 30

Extract the second (0-59) from a timestamp.

datetime.second(ts) // => 45

Get the day of week as a string (Monday-Sunday).

datetime.weekday(ts) // => "Monday"

Add n days to a timestamp.

datetime.add_days(ts, 7) // one week later

datetime.add_hours(ts: Int, n: Int) -> Int

Section titled “datetime.add_hours(ts: Int, n: Int) -> Int”

Add n hours to a timestamp.

datetime.add_hours(ts, 3)

datetime.add_minutes(ts: Int, n: Int) -> Int

Section titled “datetime.add_minutes(ts: Int, n: Int) -> Int”

Add n minutes to a timestamp.

datetime.add_minutes(ts, 30)

datetime.add_seconds(ts: Int, n: Int) -> Int

Section titled “datetime.add_seconds(ts: Int, n: Int) -> Int”

Add n seconds to a timestamp.

datetime.add_seconds(ts, 90)

datetime.diff_seconds(a: Int, b: Int) -> Int

Section titled “datetime.diff_seconds(a: Int, b: Int) -> Int”

Compute the difference in seconds between two timestamps.

datetime.diff_seconds(later, earlier) // => 3600

datetime.is_before(a: Int, b: Int) -> Bool

Section titled “datetime.is_before(a: Int, b: Int) -> Bool”

Check if timestamp a is before timestamp b.

datetime.is_before(earlier, later) // => true

Check if timestamp a is after timestamp b.

datetime.is_after(later, earlier) // => true