青春因磨砺而出彩,人生因奋斗而升华
时间格式化符号:
年份:
06
| 仅保留后两位的年份 | 19
|
2006
| 完整的年份 | 2019 |
月份
1
| 去掉前导0
| 2
|
01
| 保留前导0
| 02
|
Jan
| 英文缩写
| Feb
|
January
| 英文全写
| February |
日:
2
| 日期去除前导0
| 5
|
02
| 日期保留前导0
| 05 |
小时:
3
| 12小时制,去掉前导0 | 9
|
03
| 12小时制,保留前导0 | 09
|
15
| 24小时制,保留前导0 | 13
|
03pm
| 24小时制am/pm表示上下午,保留前导0 | 09am
|
3pm
| 24小时制am/pm表示上下午,去除前导0 | 9am |
分钟
4
| 去除前导0
| 9
|
04
| 保留前导0
| 09 |
秒钟
5
| 去除前导0
| 4
|
05
| 保留前导0
| 04 |
周:
Mon | 简写周 | Sun |
Monday | 全写周 | Sunday |
strTime := time.Now() // 当前时间
fmt.Printf("format year: %v\n", strTime.Format("06")) // 19
fmt.Printf("format year: %v\n", strTime.Format("2006")) // 2019
fmt.Printf("format month 1: %v\n", strTime.Format("1")) // 12
fmt.Printf("format month 01: %v\n", strTime.Format("01")) // 12
fmt.Printf("format month Jan: %v\n", strTime.Format("Jan")) // Dec
fmt.Printf("format month January: %v\n", strTime.Format("January")) // December
fmt.Printf("format day 2: %v\n", strTime.Format("2")) // 30
fmt.Printf("format day 02: %v\n", strTime.Format("02")) // 30
fmt.Printf("format day _2: %v\n", strTime.Format("_2")) // 30
fmt.Printf("format hour 3: %v\n", strTime.Format("3")) // 4
fmt.Printf("format hour 03: %v\n", strTime.Format("03")) // 04
fmt.Printf("format hour 15: %v\n", strTime.Format("15")) // 16
fmt.Printf("format hour 03pm: %v\n", strTime.Format("03pm")) // 04pm
fmt.Printf("format hour 3pm: %v\n", strTime.Format("3pm")) // 4pm
fmt.Printf("format min 4: %v\n", strTime.Format("4")) // 2
fmt.Printf("format min 04: %v\n", strTime.Format("04")) // 02
fmt.Printf("format week Mon: %v\n", strTime.Format("Mon")) // Mon
fmt.Printf("format week Monday: %v\n", strTime.Format("Monday")) // Monday
fmt.Printf("format time: %v\n", strTime.Format("2006-Jan-02 03pm:4:5")) // 2019-Dec-30 04pm:2:14
fmt.Printf("format time: %v\n", strTime.Format("2006-01-02 03pm:4:5")) // 2019-12-30 04pm:2:14
fmt.Printf("format time: %v\n", strTime.Format("2006-01-02 3:4:5")) // 2019-12-30 4:2:14
fmt.Printf("format time: %v\n", strTime.Format("2006-01-02 15:04:05")) // 2019-12-30 16:02:14时间函数
strTime.Year() // 年; 2020
strTime.Month() // 月; March
strTime.Day() // 天; 12
strTime.Hour() // 时; 16
strTime.Minute() // 分钟; 51
strTime.Second() // 秒; 34
strTime.Nanosecond() // 纳秒; 974969000
strTime.Unix() // 秒时间戳; 1584003094
strTime.UnixNano() // 纳秒时间戳; 1584003094974969000
time.Unix(1577689526, 0).Format("2006-01-02 15:04:05") // 时间戳转化为时间格式; 2019-12-30 15:05:26
time.Date(2019, 1, 7, 9, 5,49, 0, time.Local).Unix() // 字符串转化为时间戳; 1546823149
time.Parse("2006-01-02 15:04:05", "2019-12-12 12:00:00") // 字符串转化为time类型; 2019-12-12 12:00:00 +0000 UTC / nil
/*--------- 获取一个小时之前的时间 ---------*/
b, _ := time.ParseDuration("-1h") // 持续时间字符串,ns/us/ms/s/m/h; -1h0m0s
b1 := tTmp.Add(b) // 2020-03-12 15:57:43.035504
/*--------- 判断是否在一个时间之后 ---------*/
tT, _ := time.Parse("2006-01-02 15:04:05", "2019-12-12 12:00:00")
bafter := tT.After(time.Now()) //返回bool型, true: time.Now()在 tT 之后;flase: time.Now()在 tT 时间之前
tT.Before(time.Now()) // 是否在time.Now()时间之前
tT.Equal(time.Now()) // 判断两个时间是否相等
time.Since(tT) // 离现在过去了多长时间
tT.IsZero() // 判断时间是否为零值,如果sec/nsec两个属性都为0,则该类型为0
tT.Weekday() // 返回星期几,英文
tT.Add() // 为tT添加时间类型为Duration的时间,精确纳秒
tT.Sub(time2) // 计算tT和time2的时间差,428h40m12.92878s
tT.AddDate(year, months, days) // 以年月日形式增加时间: AddDate(year, month, days int)
eg:
tTmp := time.Now() // 2019-12-30 16:45:24.89876
tTmp.AddDate(1, 0, 0) // 2020-12-30 16:45:24.89876
tTmp.AddDate(-1, 0, 0) // 2018-12-30 16:45:24.89876