Types are used to represent a specific point in time, which can be accurate to nanoseconds. The structure definition and corresponding methods are as follows:
type Time struct { wall uint64 ext int64 loc *Location }
Methods to obtain various point-of-time attributes
- func (t Time) Date() (year int, month, day int), obtain date (year, month, day) information.
- func (t Time) Year() int, get year information.
- func (t Time) YearDay() int, get the day of the year (1~365).
- func (t Time) Month() Month, obtains month information, returns a Month type;
- func (t Time) ISOWeek() (year, week int), returns the year and week of ISO 8601 format (1-53).
- func (t Time) Weekday() Weekday, a Weekday type returned.
- func (t Time) Day() int, get the number (1~31) in the month.
- func (t Time) Clock() (hour, min, sec int), obtain time (hour, minute, second) information.
- func (t Time) Hour() int, get hour information (0~23).
- func (t Time) Minute() int, get minute information (0~59).
- func (t Time) Second() int, obtain second information (0~59).
- func (t Time) Nanosecond() int, obtain nanosecond information (0~99999999999).
- func (t Time) Unix() int64, get the second timestamp.
- func (t Time) UnixMilli() int64, get the millisecond timestamp.
- func (t Time) UnixMicro() int64, get the microsecond timestamp.
- func (t Time) UnixNano() int64, get the nanosecond timestamp.
- func (t Time) String() string, return the time format of type "2006-01-02 15:04:05.999999999 -0700 MST".
- func (t Time) Location() *Location, get time zone information.
See a simple example:
package main import ( "fmt" "time" ) func main() { t := () (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) (()) }
Time processing method (compare, judge, analysis)
- func (t Time) Format(layout string) string, formats time to the specified format.
- func (t Time) Add(d Duration) Time, plus the specified time.
- func (t Time) AddDate(years int, months int, days int) Time, returns the corresponding time point after adding the given number of years, months and days to t.
- func (t Time) Sub(u Time) Duration, returns the time difference between two time points.
- func (t Time) Truncate(d Duration) Time, truncate the specified time.
- func (t Time) Round(d Duration) Time, rounds the time to the specified time.
- func (t Time) Equal(u Time) bool, determine whether the two time points are equal.
- func (t Time) After(u Time) bool, determine whether the t time point is after the u time point.
- func (t Time) Before(u Time) bool, determine whether the t time point is before the u time point.
I won’t explain the other methods one by one, you can refer to the official documentation to view them in detail.
This is the end of this article about detailed explanation of the structures in the Golang time package. For more related contents of Golang structures, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!