💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ~~~ 2006-1-2 15:4:5 ~~~ 这个更好更兼容 # 日期格式化 ~~~ now := time.Now() //2019-05-25 12:57:06.462716 +0800 CST m=+0.000279208 fmt.Println(now) year := now.Year() month := now.Month() day := now.Day() hour := now.Hour() minute := now.Minute() second := now.Second() //2019-05-25 12:57:06 fmt.Printf("%02d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second) ~~~ 一定要是这个时间`2006-01-02 15:04:05` ~~~ fmt.Println(time.Now().Format("2006-01-02 15:04:05")) ~~~ 其他格式化 ![](https://box.kancloud.cn/c73a584a7b8311ec124a207919d9d506_774x199.png) # 时间戳 ~~~ //int64类型 t := time.Now().Unix() //当前时间戳 fmt.Println(t) //当前时间戳,取年月日 //转为时间格式 timeObj := time.Unix(t, 0) year := timeObj.Year() month := timeObj.Month() day := timeObj.Day() hour := timeObj.Hour() minute := timeObj.Minute() second := timeObj.Second() fmt.Printf("%02d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second) ~~~ # 定时器 ~~~ ticker := time.Tick(time.Second * 2) for i := range ticker { fmt.Println(i) } ~~~ # 纳秒 time.Duration用来表示纳秒 ![](https://box.kancloud.cn/0f03a8a732100065a0d76e2f99ac3c7e_959x401.png) # 延时和格式化时间转时间戳 ~~~ const base_format = "2006-1-2 15:4:5" //获取当前时间 nt := time.Now() fmt.Printf("now datetime:%v\n", nt) //延时年月日 adnt := nt.AddDate(1,2,3) fmt.Printf("now adddate:%v\n", adnt) //延时秒 ant := nt.Add(3600*1e9) //延时1小时=60*60 秒 fmt.Printf("now add:%v\n", ant) //时间字符串转时间戳 1516237261 t, _ := time.Parse(base_format, "2018-1-18 1:1:1") datetime_str_to_timestamp := t.Unix() println("datetime_str_to_timestamp:", datetime_str_to_timestamp) ~~~