Common syntax of Go Time

Common syntax of Go Time


table of Contents

  1. Get the current timestamp and formatted time of string type
  2. Construct specified time
  3. Conversion between timestamp and formatted time
  4. Get the current month, day, day of the week
  5. 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) //使用parseInLocation将字符串格式化返回本地时区时间
	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())
}

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/109560846