Skip to main content

Date Class

Date is Alia's built-in date-time class, used to represent and process date and time data. It is based on UTC time.

Creating a Date​

No Arguments (Current Time)​

const now = new Date

From a String​

const d = new Date ['2024-06-15']
const d2 = new Date ['2024-06-15 14:30:45']

Supported formats: YYYY-MM-DD or YYYY-MM-DD HH:mm:ss

From an Object​

const d = new Date [*[year: 2024, month: 6, day: 15, hour: 14, minute: 30, second: 45, millisecond: 500]]

Available fields: year, month, day, hour, minute, second, millisecond

From a Timestamp​

const d = new Date [1700000000000]

From an Array​

const arr = ['2024-06-15']
const d = new Date [arr]

The first element of the array is a date string or timestamp; that element is parsed as the date.

Instance Properties​

All properties are read-only getters:

PropertyTypeDescription
yearNumberYear (UTC)
monthNumberMonth (1-12)
dayNumberDay of month (1-31)
hourNumberHour (0-23)
minuteNumberMinute (0-59)
secondNumberSecond (0-59)
millisecondNumberMillisecond (0-999)
weekdayNumberDay of week (1=Sunday, 2=Monday, ..., 7=Saturday)
timestampNumberUnix timestamp (milliseconds)
const d = new Date ['2024-06-15 14:30:45']
print d.year ~ 2024
print d.month ~ 6
print d.day ~ 15
print d.hour ~ 14
print d.minute ~ 30
print d.second ~ 45
print d.weekday ~ 7

Instance Methods​

addTime​

Returns a new Date with the specified amount of time added.

const d = new Date ['2024-01-10']
const d2 = d.addTime 5 in Date.Days
print d2.day ~ 15

subTime​

Returns a new Date with the specified amount of time subtracted.

const d = new Date ['2024-01-20']
const d2 = d.subTime 5 in Date.Days
print d2.day ~ 15

diffTime​

Computes the difference from another Date and returns a Number.

const d1 = new Date ['2024-01-20']
const d2 = new Date ['2024-01-10']
const diff = d1.diffTime d2 in Date.Days
print diff ~ 10

toFormat​

Formats the Date as a string.

const d = new Date ['2024-01-15 09:05:03']
print d.toFormat 'YYYY-MM-DD' ~ '2024-01-15'
print d.toFormat 'YYYY/MM/DD HH:mm:ss' ~ '2024/01/15 09:05:03'
const dMs = new Date [*[year: 2024, month: 1, day: 15, hour: 9, minute: 5, second: 3, millisecond: 123]]
print dMs.toFormat 'HH:mm:ss.SSS' ~ '09:05:03.123'

isBefore / isAfter / isSame​

Compares two Dates.

const d1 = new Date ['2024-01-10']
const d2 = new Date ['2024-01-20']
print d1.isBefore d2 ~ true
print d1.isAfter d2 ~ false
print d1.isSame d1 ~ true

isLeapYear​

Checks whether the year is a leap year. Zero-argument methods are invoked via call and return a Boolean.

const d = new Date ['2024-01-01']
print call d.isLeapYear ~ true

daysInMonth​

Gets the number of days in the current month. Zero-argument methods are invoked via call and return a Number.

const d = new Date ['2024-02-01']
print call d.daysInMonth ~ 29

Time Unit Constants​

ConstantValue
Date.Years'Year'
Date.Months'Month'
Date.Weeks'Week'
Date.Days'Day'
Date.Hours'Hour'
Date.Minutes'Minute'
Date.Seconds'Second'
Date.Milliseconds'Millisecond'
const d = new Date ['2024-01-10']
const d2 = d.addTime 3 in Date.Months
const diff = d2.diffTime d in Date.Days

Static Methods​

Date.now​

Gets the current timestamp. Zero-argument methods are invoked via call and return a Number.

const ts = call Date.now

Date.parseDate​

Parses a date from a string.

const d = Date.parseDate '2024-06-15'
print d.year ~ 2024

Date.fromTimestamp​

Creates a Date from a timestamp.

const d = Date.fromTimestamp 1700000000000

Date.fromParts​

Creates a Date from individual parts. year and month are required keywords; day, hour, minute, second, and millisecond are optional keywords.

const d = Date.fromParts year 2024 month 6 day 15 hour 14 minute 30 second 45

Date.leapYear​

Checks whether the specified year is a leap year.

print Date.leapYear 2024 ~ true
print Date.leapYear 2023 ~ false

Date.daysInMonthStatic​

Gets the number of days in the specified year and month.

print Date.daysInMonthStatic year 2024 month 2 ~ 29

Format Patterns​

TokenDescriptionExample
YYYYFour-digit year2024
MMTwo-digit month01
DDTwo-digit day15
HHTwo-digit hour09
mmTwo-digit minute05
ssTwo-digit second03
SSSThree-digit millisecond123
WWWeekday number2
DDnameWeekday nameMonday
OOMonth nameJanuary