DateTime()
Utility class to perform datetime calculations
Signature
Creates a new DateTime instance representing the current date and time.
DateTime();
Creates a new DateTime instance from a timestamp in milliseconds.
DateTime(timestamp: number)
Creates a new DateTime instance by parsing a date string.
DateTime(dateString: string)
Creates a new DateTime instance for the specified date at midnight (00:00:00).
DateTime(year: number, month: number, day: number)
Creates a new DateTime instance for the specified date and time.
DateTime(year: number, month: number, day: number, hour: number, minute: number, second: number)
Parameters
Parameter | Type | Description |
---|---|---|
timestamp | number | timestamp in milliseconds since epoch |
dateString | string | string representation of date to parse |
year | number | year value (e.g. 2024) |
month | number | month value (1-12) |
day | number | day of month (1-31) |
hour | number | hour of day (0-23) |
minute | number | minute of hour (0-59) |
second | number | second of minute (0-59) |
Returns
- Type:
DateTime
- A new
DateTime
instance.
Examples
-- Create a DateTime object for the current date and time
local now = DateTime()
toast("Current DateTime: " .. now:format("yyyy-MM-dd HH:mm:ss"))
-- Create a DateTime object from a timestamp
local timestamp = 1678886400000 -- March 15, 2023 00:00:00 UTC
local dateFromTimestamp = DateTime(timestamp)
toast("DateTime from timestamp: " .. dateFromTimestamp:format("yyyy-MM-dd HH:mm:ss"))
-- Create a DateTime object from a date string
local dateFromString = DateTime("2023-03-15 10:30:00")
toast("DateTime from string: " .. dateFromString:format("yyyy-MM-dd HH:mm:ss"))
-- Create a DateTime object for a specific date
local specificDate = DateTime(2023, 3, 15)
toast("Specific Date: " .. specificDate:format("yyyy-MM-dd HH:mm:ss"))
-- Create a DateTime object for a specific date and time
local specificDateTime = DateTime(2023, 3, 15, 14, 45, 30)
toast("Specific DateTime: " .. specificDateTime:format("yyyy-MM-dd HH:mm:ss"))