go time包使用总结

初始化Time对象

当前时间
func Now() Time
//返回当前时间
固定时间
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
//用年、月、日、时、分、秒、纳秒、地区构造时间对象
从字符串解析
func Parse(layout, value string) (Time, error)
//value:要解析的字符串
//layout:解析格式
//e.g.
t1, _ := time.Parse("2006-01-02", "2020-10-01")
t2, _ := time.Parse("20060102", "20201002")
t3, _ := time.Parse(time.RFC3339, t1.Format(time.RFC3339))
fmt.Println(t1)
fmt.Println(t2)
fmt.Println(t3)
/*stdout:
2020-10-01 00:00:00 +0000 UTC
2020-10-02 00:00:00 +0000 UTC
2020-10-01 00:00:00 +0000 UTC
*/

layout中,代表年、月、日、时、分、秒的数字是一个固定值,不能随意替换

记得方式是“月 日 时 分 秒 年”对应“01 02 03 04 05 06”,03对应下午,要换成15

所以常用的日期格式就是2016-01-02"2006-01-02 15:04:05"

如果要解析成当地时间(带时区信息),要用ParseInLocation

func ParseInLocation(layout, value string, loc *Location) (Time, error)
//e.g.
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", "2020-10-05 12:22:22", time.Local)
fmt.Println(t1)
fmt.Println(t1.UTC())
/*stdout
2020-10-05 12:22:22 +0800 CST
2020-10-05 04:22:22 +0000 UTC
*/

Time对象的方法

转字符串
func (t Time) Format(layout string) string
//按照layout的格式转换成字符串格式
日期比较
func (t Time) After(u Time) bool
//如果t在u之后返回True
//e.g.
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-05 12:22:22")
t2 := time.Now()
t3 := time.Date(2020, 9, 15, 12, 12, 12, 0, time.Local)
fmt.Println(t1.Format("2006-01-02 15:03:05"))
fmt.Println(t2.Format("2006-01-02 15:03:05"))
fmt.Println(t3.Format("2006-01-02 15:03:05"))
fmt.Println(t1.After(t2))
fmt.Println(t2.After(t3))
fmt.Println(t3.After(t1))
/*stdout:
2020-10-05 12:12:22
2020-09-08 11:11:54
2020-09-15 12:12:12
true
false
false
*/

类似地还有BeforeEqual

func (t Time) Before(u Time) bool
func (t Time) Equal(u Time) bool
日期加减

对日期对象加上时间间隔得到新日期

func (t Time) Add(d Duration) Time
//在接收器的时间上增加d的时间间隔,返回一个新的Time对象
//如果想向前移时间,可以传负数
//e.g.
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-05 12:22:22")
t1 = t1.Add(time.Hour*24)
fmt.Println(t1.Format("2006-01-02 15:04:05"))
t1 = t1.Add(-time.Hour*48)
fmt.Println(t1.Format("2006-01-02 15:04:05"))
/*stdout
2020-10-06 12:22:22
2020-10-04 12:22:22
*/

对日期对象加上日月年得到新日期

func (t Time) AddDate(years int, months int, days int) Time

两个日期相减计算时间间隔

func (t Time) Sub(u Time) Duration
//返回t-u的时间差
//e.g.
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-05 12:22:22")
t2 := time.Now()
d := t1.Sub(t2)
fmt.Println(d)
/*stdout
653h57m47.150782s
*/

上边的例子可以用Until简写,t减去当前时间

func Until(t Time) Duration {
	var now Time
	if t.wall&hasMonotonic != 0 {
		// Common case optimization: if t has monotonic time, then Sub will use only it.
		now = Time{hasMonotonic, runtimeNano() - startNano, nil}
	} else {
		now = Now()
	}
	return t.Sub(now)
}
//e.g.
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-05 12:22:22")
d := Until(t1)

类似地还有Since,当前时间减去t

func Since(t Time) Duration {
	var now Time
	if t.wall&hasMonotonic != 0 {
		// Common case optimization: if t has monotonic time, then Sub will use only it.
		now = Time{hasMonotonic, runtimeNano() - startNano, nil}
	} else {
		now = Now()
	}
	return now.Sub(t)
}
转时间戳
func (t Time) Unix() int64
//e.g.
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", "2020-10-05 12:22:22", time.Local)
fmt.Println(t1.Unix())
获取日期信息
func (t Time) Date() (year int, month Month, day int) //返回年月日,其中月是Month类型,可以转为int
func (t Time) Year() int
func (t Time) Month() Month
func (t Time) Day() int
func (t Time) Hour() int
func (t Time) Minute() int
func (t Time) Second() int
func (t Time) Nanosecond() int
func (t Time) Weekday() Weekday
func (t Time) ISOWeek() (year, week int) //返回年和周数
func (t Time) YearDay() int
时区转换
func (t Time) UTC() Time
func (t Time) Local() Time
func (t Time) In(loc *Location) Time
func (t Time) Location() *Location //返回日期时区信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值