Conversion Between DateTime and String
This example demonstrates how to format time by formatting the string pattern
and parse time from the formatted string.
Notes:
In this example, the TimeZone.load function is used to load the time zone information. Different dependencies exist in loading time zone information on different platforms. Setting needs to be performed as required.
import std.time.*
main() {
let pattern = "yyyy/MM/dd HH:mm:ssSSS OO"
let datetime = DateTime.of(
year: 2024,
month: May,
dayOfMonth: 22,
hour: 12,
minute: 34,
second: 56,
nanosecond: 789000000,
timeZone: TimeZone.load("Asia/Shanghai")
)
let str = datetime.toString(pattern)
println(str)
println(DateTime.parse(str, pattern))
}
Running result:
2024/05/22 12:34:56789000000 +08:00
2024-05-22T12:34:56.789+08:00