格式化日期时间
第一种方式: fmt.Sprintf() 函数
now := time.Now()
fmt.Printf("now-> %v, type-> %T
", now, now) // now-> 2021-08-30 21:59:14.679666 +0800 CST m=+0.000092171, type-> time.Time
dateTimeStr := fmt.Sprintf("%d-%d-%d %d:%d:%d",now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
dateStr := fmt.Sprintf("%d-%d-%d", now.Year(), now.Month(), now.Day())
timeStr := fmt.Sprintf("%d:%d:%d", now.Hour(), now.Minute(), now.Second())
fmt.Println(dateTimeStr) // 2021-8-30 21:59:14
fmt.Println(dateStr) // 2021-8-30
fmt.Println(timeStr) // 21:59:14
now := time.Now()
fmt.Printf("now-> %v, type-> %T
", now, now) // now-> 2021-08-30 21:59:14.679666 +0800 CST m=+0.000092171, type-> time.Time
dateTimeStr := now.Format("2006-01-02 15:04:05") // 各个数字是固定的,组合可以自由
dateStr := now.Format("2006-01-02")
timeStr := now.Format("15:04:05")
fmt.Println(dateTimeStr) // 2021-8-30 21:59:14
fmt.Println(dateStr) // 2021-8-30
fmt.Println(timeStr) // 21:59:14