1.获取时间
1.1 当前时间
unix := time.Now().Unix() fmt.Printf("当前时间戳(单位秒): %v \n",unix) nano := time.Now().UnixNano() fmt.Printf("当前时间戳(单位纳秒): %v\n",nano) format := time.Now().Format("2006-01-02 15:04:05") fmt.Printf("当前时间(Y-m-d H:i:s): %v\n",format) format2 := time.Now().Format("20060102150405") fmt.Printf("当前时间(YmdHis): %v\n",format2)
|
1.2 当前年、月、日、时、分、秒、星期几
format := time.Now().Format("2006-01-02 15:04:05") fmt.Printf("当前时间(Y-m-d H:i:s): %v\n",format) fmt.Printf("当前年: %v\n", time.Now().Year()) fmt.Printf("当前月: %v\n", time.Now().Month()) fmt.Printf("当前日: %v\n", time.Now().Day()) fmt.Printf("当前小时: %v\n", time.Now().Hour()) fmt.Printf("当前分钟: %v\n", time.Now().Minute()) fmt.Printf("当前秒: %v\n", time.Now().Second()) fmt.Printf("当前星期几: %v\n", time.Now().Weekday())
|
1.3 获取时分秒
now := time.Now() hour, min, sec := now.Clock() fmt.Printf("时间:%v huor: %v min:%v sec:%v \n",now,hour,min,sec)
|
1.4 创建指定时间
date := time.Date(2020,8,23,13,53,32,0,time.Local) fmt.Printf("类型: %T , %v ",date,date)
|
1.5 获取所属第几周
week, w := time.Now().ISOWeek() fmt.Printf("year: %v w:%v \n",week,w)
|
1.6 获取x年的第几天
num := time.Now().YearDay() fmt.Printf("今天是今年的第 %v 天 \n",num)
|
2.字符串转时间
2.1 字符串转时间类型
str := "1616319808" unix, _ := strconv.ParseInt(str,10,64) format := time.Unix(unix, 0) fmt.Printf("字符串时间戳-> 类型:%T 值:%v \n",format,format)
strDate := "2019-04-10 12:54:03"
location, _ := time.Parse("2006-01-02 15:04:05", strDate) fmt.Printf("字符串时间-> 类型:%T 值:%v \n",location,location)
|
2.2 格式化时间
Format
根据layout
指定的格式返回t
代表的时间点的格式化文本表示;Format
中内置了几个固定的layout
常量。
const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )
|
使用:
strDate := "2020-09-12 14:34:10"
location, _ := time.ParseInLocation("2006-01-02 15:04:05", strDate, time.Local)
format := location.Format("20060102150405") fmt.Println(format)
|
3.时间比较
strDate := "2020-09-12 12:00:00" strDate2 := "2020-09-12 12:00:00"
time1, _ := time.ParseInLocation("2006-01-02 15:04:05", strDate, time.Local) time2, _ := time.ParseInLocation("2006-01-02 15:04:05", strDate2, time.Local)
before := time1.Before(time2) fmt.Printf("strDate2 > strDate ? %t \n",before)
equal := time1.Equal(time2) fmt.Printf("strDate2 = strDate ? %t \n",equal)
|
4.时间计算
4.1 时间相加
now := time.Now() fmt.Printf("现在的时间: %v \n",now)
duration, _ := time.ParseDuration("-10m") fmt.Printf("十分钟前: %v \n", now.Add(duration))
duration2, _ := time.ParseDuration("-1h") fmt.Printf("一小时前: %v \n", now.Add(duration2))
fmt.Printf("一天后: %v \n", now.AddDate(0,0,1)) fmt.Printf("一月后: %v \n", now.AddDate(0,1,0)) fmt.Printf("一年后: %v \n", now.AddDate(1,0,0))
|
4.2 时间相减
day1, _ := time.ParseInLocation("2006-01-02 15:04:05", "2020-11-02 12:00:00", time.Local) day2, _ := time.ParseInLocation("2006-01-02 15:04:05", "2020-11-02 15:00:00", time.Local) fmt.Printf("相差几秒: %v \n",day2.Sub(day1).Seconds()) fmt.Printf("相差多少分钟: %v \n",day2.Sub(day1).Minutes()) fmt.Printf("相差多少小时: %v \n",day2.Sub(day1).Hours())
|
5.定时器
5.1 NewTicker
NewTicker
返回一个新的Ticker
,该Ticker
包含一个通道字段,并会每隔时间段d
就向该通道发送当时的时间。它会调整时间间隔或者丢弃tick
信息以适应反应慢的接收者。如果d<=0会panic
。关闭该Ticker
可以释放相关资源。
** 使用示例**
func main() { ticker := time.NewTicker(time.Second) go func(ticker *time.Ticker) { for i := 0; i < 3; i++ { fmt.Println(<-ticker.C) } ticker.Stop() }(ticker) time.Sleep(3 * time.Second) fmt.Println("end") }
|
5.2 Tick
Tick
是NewTicker
的封装,只提供对Ticker
通道的访问。如果不需要关闭Ticker
,本函数就很方便。
func main() { chTime := time.Tick(time.Second) go func(ch <- chan time.Time) { for i := 0; i < 3; i++ { fmt.Println(<-ch) } }(chTime) time.Sleep(4 * time.Second) fmt.Println("end") }
|
6.延迟执行
6.1 NewTimer
NewTimer
创建一个Timer
,它会在指定时间后,向其自身的C
字段发送当时的时间。
func main() { fmt.Printf("开始时间: %v \n",time.Now()) timer := time.NewTimer(time.Second * 3) fmt.Printf("timer通道里的时间: %v \n", <- timer.C) }
|
6.2 After
After()
函数相当于NewTimer(d). C
,如下源码:
func After(d Duration) <-chan Time { return NewTimer(d).C }
|
使用示例
func main() { ch := time.After(5 * time.Second) fmt.Printf("开始时间 %v \n",time.Now()) out := <- ch fmt.Printf("变量out-> 类型: %T 值:%v \n",out,out) fmt.Printf("开始时间 %v \n",time.Now()) }
|