Skip to main content

Date & Time

Utah provides a date.* namespace for working with dates, times, and Unix timestamps using the system date command.

Getting the Current Time

Unix Timestamp (seconds)

let ts: number = date.now()
console.log("Current timestamp: ${ts}")

Compiles to:

ts=$(date +%s)
echo "Current timestamp: ${ts}"

Unix Timestamp (milliseconds)

let ms: number = date.nowMillis()
console.log("Current time in ms: ${ms}")

Compiles to:

ms=$(date +%s%3N)
echo "Current time in ms: ${ms}"

Formatting Dates

Use date.format() to convert timestamps into human-readable strings.

// Format current time with default format
let now: string = date.format()

// Format a specific timestamp with default format
let ts: number = date.now()
let formatted: string = date.format(ts)

// Format with a custom format string
let custom: string = date.format(ts, "%Y-%m-%d")

Common Format Specifiers

SpecifierDescriptionExample
%Y4-digit year2024
%mMonth (01-12)03
%dDay (01-31)15
%HHour (00-23)14
%MMinute (00-59)30
%SSecond (00-59)45
%ADay nameMonday
%BMonth nameMarch
%sUnix timestamp1710000000

Parsing Date Strings

Convert a date string into a Unix timestamp with date.parse().

let ts: number = date.parse("2024-01-15")
console.log("Timestamp: ${ts}")

// With a custom output format
let formatted: string = date.parse("2024-01-15", "%Y-%m-%d %H:%M:%S")

Calculating Differences

Use date.diff() to find the difference between two timestamps.

let start: number = date.parse("2024-01-01")
let end: number = date.parse("2024-01-31")

// Difference in seconds (default)
let diffSec: number = date.diff(end, start)

// Difference in specific units
let diffDays: number = date.diff(end, start, "days")
let diffHours: number = date.diff(end, start, "hours")
let diffMinutes: number = date.diff(end, start, "minutes")

Supported Units

UnitDescription
"seconds"Difference in seconds
"minutes"Difference in minutes
"hours"Difference in hours
"days"Difference in days

Timestamp Arithmetic

Adding Time

let now: number = date.now()

// Add 7 days
let nextWeek: number = date.add(now, 7, "days")

// Add 2 hours
let later: number = date.add(now, 2, "hours")

// Add 30 minutes
let soon: number = date.add(now, 30, "minutes")

Subtracting Time

let now: number = date.now()

// Subtract 1 day
let yesterday: number = date.subtract(now, 1, "days")

// Subtract 3 hours
let earlier: number = date.subtract(now, 3, "hours")

Day of the Week

Get the name of the day for the current time or a specific timestamp.

// Current day
let today: string = date.dayOfWeek()
console.log("Today is: ${today}")

// Day for a specific timestamp
let ts: number = date.parse("2024-01-15")
let day: string = date.dayOfWeek(ts)
console.log("That day was: ${day}")

Complete Example

// Measure script execution time
let startTime: number = date.now()
let startFormatted: string = date.format(startTime)
console.log("Script started at: ${startFormatted}")

// Do some work...
console.log("Processing...")

let endTime: number = date.now()
let elapsed: number = date.diff(endTime, startTime)
console.log("Elapsed: ${elapsed} seconds")

// Schedule a future event
let deadline: number = date.add(startTime, 24, "hours")
let deadlineStr: string = date.format(deadline, "%Y-%m-%d %H:%M")
console.log("Deadline: ${deadlineStr}")

// Check what day it is
let day: string = date.dayOfWeek()
console.log("Today is ${day}")

API Reference

FunctionParametersReturnsDescription
date.now()numberCurrent Unix timestamp (seconds)
date.nowMillis()numberCurrent Unix timestamp (milliseconds)
date.format(ts?, fmt?)ts: timestamp, fmt: format stringstringFormat timestamp as string
date.parse(str, fmt?)str: date string, fmt: output formatnumber|stringParse date string
date.diff(ts1, ts2, unit?)ts1, ts2: timestamps, unit: time unitnumberDifference between timestamps
date.add(ts, amt, unit)ts: timestamp, amt: amount, unit: time unitnumberAdd time to timestamp
date.subtract(ts, amt, unit)ts: timestamp, amt: amount, unit: time unitnumberSubtract time from timestamp
date.dayOfWeek(ts?)ts: timestampstringDay of the week name