datetime
This content is for v0.2.1. Switch to the latest version for up-to-date documentation.
The datetime namespace provides functions to work with system time, timestamps, and date formatting.
Quick Reference
Section titled “Quick Reference”| Function | Signature | Return Type | Description |
|---|---|---|---|
datetime.now | datetime.now() | object | Returns current date/time component object. |
datetime.timestamp | datetime.timestamp() | number | Returns Unix timestamp in seconds (floating point). |
datetime.format | datetime.format(ts, pattern) | string | Formats a timestamp using pattern format specifiers. |
Detailed Methods
Section titled “Detailed Methods”datetime.now()
Section titled “datetime.now()”Returns an object containing calendar and time components:
var dt = datetime.now();print("Year: " + str(dt["year"]));print("Month: " + str(dt["month"]));print("Day: " + str(dt["day"]));print("Hour: " + str(dt["hour"]));print("Minute: " + str(dt["minute"]));print("Second: " + str(dt["second"]));print("Millisecond: " + str(dt["ms"]));print("Timestamp: " + str(dt["timestamp"]));datetime.timestamp()
Section titled “datetime.timestamp()”Returns high-precision Unix timestamp in seconds (with fractional sub-second precision):
var start = datetime.timestamp();
var sum = 0;var i = 0;while (i < 100000) { sum = sum + i; i = i + 1;}
var elapsed = datetime.timestamp() - start;print("Computation took: " + str(elapsed) + " seconds");datetime.format(timestamp, pattern)
Section titled “datetime.format(timestamp, pattern)”Formats a timestamp into a custom string representation:
| Specifier | Meaning | Example |
|---|---|---|
%Y | 4-digit Year | 2026 |
%m | 2-digit Month | 09 |
%d | 2-digit Day | 12 |
%H | 24-hour format | 14 |
%M | Minute | 35 |
%S | Second | 02 |
var ts = datetime.timestamp();var date_str = datetime.format(ts, "%Y-%m-%d %H:%M:%S");print("Current time: " + date_str);