Dates & Time
The Date class provides comprehensive tools for working with dates, times, and timestamps. Since it is part of the standard modules, you must explicitly import it using @import("std/date").
Creating Dates
The Date class is marked with @no_constructor, meaning you cannot create it using Date(). Instead, you use the static factory methods now() or fromTimestamp().
@import("std/date")
// Get the current date and time
val currentDate = Date.now()
// Create a date from a specific Unix timestamp (seconds)
val pastDate = Date.fromTimestamp(1609459200)
// Get the current timestamp in milliseconds directly
val ms = Date.currentTimeMillis()
println("Current Milliseconds: ${ms}")Retrieving Date Components
Once you have a Date object, you can easily extract individual components such as the year, month, day, or time directly using getter methods.
@import("std/date")
val today = Date.now()
println("Year: ${today.getYear()}")
println("Month: ${today.getMonth()}")
println("Day: ${today.getDay()}")
println("Time: ${today.getHours()}:${today.getMinutes()}:${today.getSeconds()}")
println("Is Leap Year? ${if (today.isLeapYear()) "Yes" else "No"}")Formatting Dates
The format() method allows you to convert a Date into a String using standard C++ formatting patterns (like %Y for Year, %m for Month, etc.).
@import("std/date")
val now = Date.now()
// Standard SQL datetime format
val sqlFormat = now.format("%Y-%m-%d %H:%M:%S")
println("SQL Format: ${sqlFormat}")
// Custom readable format
val custom = now.format("%B %d, %Y - %I:%M %p")
println("Custom: ${custom}")Manipulating Dates
You can easily perform date arithmetic by adding or subtracting days, hours, minutes, or seconds. These methods calculate the new time automatically and return a Date object.
@import("std/date")
val now = Date.now()
// Add 5 days
val futureDate = now.addDays(5)
println("5 days from now: ${futureDate.format("%Y-%m-%d")}")
// Subtract 2 hours (by passing a negative value)
val pastTime = now.addHours(-2)
println("2 hours ago: ${pastTime.format("%H:%M:%S")}")Methods Reference
Static Methods (Constructors & Utils)
Date.now(): Date- Returns a Date object representing the current local time.Date.fromTimestamp(timestamp: Int): Date- Creates a Date from a Unix timestamp (in seconds).Date.currentTimeMillis(): Int- Returns the current time in milliseconds since the Unix epoch.
Getters
getYear(): Int- Returns the year (e.g., 2024).getMonth(): Int- Returns the month.getDay(): Int- Returns the day of the month.getHours(): Int- Returns the hour (0-23).getMinutes(): Int- Returns the minute (0-59).getSeconds(): Int- Returns the second (0-59).getTime(): Int- Returns the Unix timestamp (in seconds) of this date.isLeapYear(): Bool- Returnstrueif the date falls in a leap year.
Formatting & Manipulation
format(pattern: String): String- Formats the date to a string using standard C++ formatting patterns.addDays(days: Int): Date- Adds (or subtracts) days from the date.addHours(hours: Int): Date- Adds (or subtracts) hours from the date.addMinutes(minutes: Int): Date- Adds (or subtracts) minutes from the date.addSeconds(seconds: Int): Date- Adds (or subtracts) seconds from the date.
