golang学习笔记系列之标准库time的学习

本文介绍了Go语言中time包的基本使用,包括获取当前时间、时间戳转换、时间运算、Ticker和Timer的使用,以及时间格式化和解析。通过示例展示了如何操作和展示时间,以及如何设置定时任务和进行时间比较。
摘要由CSDN通过智能技术生成

time

Package time provides functionality for measuring and displaying time.(用于时间的测量和显示)

基本使用
//获取当前时间
	now := time.Now()
	fmt.Printf("now: %v\n", now)
	year := now.Year()          //年
	month := now.Month()        //月
	day := now.Day()            //日
	hour := now.Hour()          //时
	minute := now.Minute()      //分
	second := now.Second()      //秒
	nsecond := now.Nanosecond() //纳秒
	fmt.Printf("%v-%v-%v:%v:%v:%v:%v\n", year, month, day, hour, minute, second, nsecond)

运行结果

now: 2022-11-14 21:53:00.435701792 +0800 CST m=+0.000115447
2022-November-14:21:53:0:435701792
时间戳

时间戳是自1970年1月1日(08:00:00GMT)至当前时间的总秒数。它也被称为Unix时间戳。

//时间戳
fmt.Printf("now.Unix(): %v\n", now.Unix())           //秒数
fmt.Printf("now.UnixMicro(): %v\n", now.UnixMicro()) //毫秒
fmt.Printf("now.UnixNano(): %v\n", now.UnixNano())   //纳秒

//时间戳转成时间
fmt.Printf("time.Unix(now.Unix(), 0): %v\n", time.Unix(now.Unix(), 0))

运行结果

now.Unix(): 1668434792
now.UnixMicro(): 1668434792741556
now.UnixNano(): 1668434792741556203
time.Unix(now.Unix(), 0): 2022-11-14 22:06:32 +0800 CST
时间的"运算"
//时间的运算
	today := now
	tomorrow := today.Add(time.Hour * 24) //在当前时间上增加24小时
	fmt.Printf("today: %v\n", today)
	fmt.Printf("tomorrow: %v\n", tomorrow)

	dif := today.Sub(tomorrow) //今天与昨天相差的时间
	fmt.Printf("dif: %v\n", dif)

	//比较两个时间是否相同
	fmt.Printf("today.Equal(tomorrow): %v\n", today.Equal(tomorrow))

	//判断当前时间是否在目标时间之前
	fmt.Printf("today.Before(tomorrow): %v\n", today.Before(tomorrow))

	//判断当前时间是否在目标时间之后
	fmt.Printf("today.After(tomorrow): %v\n", today.After(tomorrow))

运行结果

today: 2022-11-14 22:16:35.620284622 +0800 CST m=+0.000048094
tomorrow: 2022-11-15 22:16:35.620284622 +0800 CST m=+86400.000048094
dif: -24h0m0s
today.Equal(tomorrow): false
today.Before(tomorrow): true
today.After(tomorrow): false
Ticker和Timer
//定时器Ticker
	c := time.Tick(time.Second) //设置一个间隔一秒的定时器
	for i := range c {
		fmt.Printf("now: %v\n", i)             //每隔一秒打印一下当前时间
		if i.After(now.Add(time.Second * 3)) { //3秒后停止
			break
		}
	}

	//Timer
	t := time.NewTimer(time.Second * 2)
	fmt.Printf("now2: %v\n", time.Now())
	<-t.C
	fmt.Printf("now2 after 2 seconds: %v\n", time.Now()) //两秒后执行到这里

运行结果

now: 2022-11-14 22:34:08.697290905 +0800 CST m=+1.000982288
now: 2022-11-14 22:34:09.697399128 +0800 CST m=+2.001090512
now: 2022-11-14 22:34:10.697623176 +0800 CST m=+3.001314560
now2: 2022-11-14 22:34:10.697739262 +0800 CST m=+3.001430647
now2 after 2 seconds: 2022-11-14 22:34:12.698702444 +0800 CST m=+5.002393830
时间格式化

时间类型有一个自带的Format方法,需要注意的是Go语言中格式化的模板不是常见的Y-m-d H:M:S,而是使用Go的诞生时间2006年1月2号15点04分(记忆口诀为:2006 1 2 3 4)。

//时间格式化
fmt.Printf("now.Format(\"2006-01-02 15:04:05.000 Mon Jan\"): %v\n", now.Format("2006/01/02 15:04:05.000 Mon Jan")) //24小时制
fmt.Printf("now.Format(\"Mon Jan 2006-01-02 3:4:4 PM\"): %v\n", now.Format("Mon Jan 2006-01-02 3:4:4.000 PM"))     //12小时制

运行结果

now.Format("2006-01-02 15:04:05.000 Mon Jan"): 2022/11/14 22:44:56.882 Mon Nov
now.Format("Mon Jan 2006-01-02 3:4:4 PM"): Mon Nov 2022-11-14 10:44:44.882 PM
解析字符串格式的时间
//解析字符串格式的时间
loc, _ := time.LoadLocation("Asia/Shanghai")

//第一个参数指定格式,第二参数为字符串格式的时间,第三个参数指定时区
t2, _ := time.ParseInLocation("2006/01/02 15:04:05.000 Mon Jan", "2022/11/11 22:44:56.882 Mon Nov", loc)
fmt.Printf("t2: %T\n", t2)
fmt.Printf("t2: %v\n", t2)

运行结果

t2: time.Time
t2: 2022-11-11 22:44:56.882 +0800 CST

同步更新于个人博客系统:golang学习笔记系列之标准库time的学习

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值