The built-in Time type has been deprecated. Please use the SDK-based Time type instead.
Import the time module from the SDK:
import "sdk/time"
The SDK provides the Time struct with equivalent functionality:
import "sdk/time"
// Get current time
var now: Time = Time.now()
// Format and display
print($"Current time: {now.format("YYYY-MM-DD HH:mm:ss")}\n")
// Time arithmetic
var later: Time = now.addHours(2)
print($"Two hours from now: {later.toIso()}\n")
// Measure elapsed time
var start: Time = Time.now()
doSomeWork()
var elapsed: int = Time.now().diff(start)
print($"Elapsed: {elapsed}ms\n")
See the SDK time module documentation for the complete API reference:
Time.now()- Get current local timeTime.utc()- Get current UTC timeTime.fromMillis(ms)- Create from epoch millisecondsTime.fromSeconds(s)- Create from epoch secondsTime.sleep(ms)- Sleep for milliseconds
Instance methods:
millis(),seconds()- Get epoch timeyear(),month(),day()- Get date componentshour(),minute(),second()- Get time componentsweekday()- Get day of weekformat(pattern),toIso()- Formattingadd(ms),addSeconds(),addMinutes(),addHours(),addDays()- Arithmeticdiff(other),isBefore(),isAfter(),equals()- Comparison
- Date - Calendar date operations
- SDK Overview - All SDK modules
- SDK source:
sdk/time.sn