The time package provides time-related types, including date and time, time interval, monotonic time, and time zone, and provides functionality of calculation and comparison.
Parsing time from a string has the following requirements:
The string must contain the information about the specific year, month, and day, for example, year (y) + month (M) and day (d), or year (y) and day of the year (D).
If information about the hour, minute, and second is not contained, the default value 0 is used. If information about the time zone is not contained, the default value of TimeZone.Local is used.
It is not allowed to assign a value twice to the formats represented by the same letter. For example, the year (y) format cannot be assigned a value twice. The O and Z formats cannot be used together to indicate the time zone.
The format of some other letters is used as auxiliary information for verification. For example, when "2023-04-24-Mon" is parsed using the "yyyy-MM-dd-www" format, the correctness of "Mon" is verified.
A year in four digits, for example, 2023 and 0001.
A year in at least four digits, for example, 2023, 0001, and 99999. Zeros are added if there are less than four digits.
2
A year in two digits, for example, 23 and 69. If xx is greater than or equal to 69, the year is parsed as 19xx; otherwise, the year is parsed as 20xx. The same rule applies to negative numbers.
A year in two or more digits, for example, 23 and 69. The output of [1969, 1999] is [69, 99], and the output of [2000, 2068] is [00, 68]. The same rule applies to negative numbers. In other cases, the output contains at least four digits. Zeros are added if there are less than four digits.
5–9
A year in the corresponding number of digits.
A year in the corresponding number of digits. Zeros are added if the number of digits is less than the expected.
Indicates the time zone. It records the time offsets of a region relative to the zero time zone at different times. It provides functionality such as loading a time zone from the system and customizing a time zone.
Indicates a day of a week. It provides functionality of Int64 type conversion, equality judgment, and obtaining the string representation of enumerated values.
Indicates a month, that is, a month in a year. It provides functionality of Int64 type conversion and calculation, equality judgment, and obtaining the string representation of enumerated values.
Indicates a date-time, which is a time type describing a point of time. It provides functionality such as time zone-based date-time reading, calculation, comparison, and conversion, as well as serialization and deserialization.
Indicates a time interval, which is a time type describing a period of time. It provides common static instances and functionality such as calculation and comparison.
Indicates monotonic time, which is a time type used to measure the elapsed time. Similar to an ever-running stopwatch, it provides functionality such as obtaining current time, calculation, and comparison.