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:
| Property | Type | Description |
|---|---|---|
year | Number | Year (UTC) |
month | Number | Month (1-12) |
day | Number | Day of month (1-31) |
hour | Number | Hour (0-23) |
minute | Number | Minute (0-59) |
second | Number | Second (0-59) |
millisecond | Number | Millisecond (0-999) |
weekday | Number | Day of week (1=Sunday, 2=Monday, ..., 7=Saturday) |
timestamp | Number | Unix 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
| Constant | Value |
|---|---|
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
| Token | Description | Example |
|---|---|---|
YYYY | Four-digit year | 2024 |
MM | Two-digit month | 01 |
DD | Two-digit day | 15 |
HH | Two-digit hour | 09 |
mm | Two-digit minute | 05 |
ss | Two-digit second | 03 |
SSS | Three-digit millisecond | 123 |
WW | Weekday number | 2 |
DDname | Weekday name | Monday |
OO | Month name | January |