Common syntax of Go Time
table of Contents
- Get the current timestamp and formatted time of string type
- Construct specified time
- Conversion between timestamp and formatted time
- Get the current month, day, day of the week
- To be continued, what is used to supplement what
1. Get the current timestamp and formatted time of string type
1. Get the current timestamp
func GetCurTimestamp() int64 {
return time.Now().Unix()
}
2. Get formatting time
func GetCurTimeFormat() string {
return time.Now().Format("2006-01-02 15:04:05")
}
2. Construct the specified time
func GetAppointTime() string {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.Local).Format("2006-01-02 15:04:05")
}
3. Convert between timestamp and formatted time
1. Timestamp to formatted time
func GetUnix2Format(timestamp int64) string {
return time.Unix(timestamp,0).Format("2006-01-02 15:04:05")
}
2. Format time to time stamp
func GetFormat2Unix(formatTime string) int64 {
unixTime, _ := time.ParseInLocation("2006-01-02 15:04:05", formatTime, time.Local)
return unixTime.Unix()
}
4. Get the current month, day, and day of the week
func GetMonthNum() int {
return int(time.Now().Month())
}
func GetDayNum() int {
return time.Now().Day()
}
func GetWeekNum() int {
return int(time.Now().Weekday())
}