当前位置:网站首页>go time包常用函数
go time包常用函数
2022-07-07 21:53:00 【咳咳,您好请多指教!】
now := time.Now() // 获取当前时间
fmt.Printf("当前时间%v\n", now)
year := now.Year() // 年
month := now.Month() // 月
day := now.Day() // 日
hour := now.Hour() // 小时
minute := now.Minute() // 分钟
second := now.Second() // 秒
secondsEastOfUTC := int((8 * time.Hour).Seconds())
beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
// fmt.Print(beijing.String())
// 北京时间 2022-02-22 22:22:22.000000022 +0800 CST
t := time.Date(2022, 02, 22, 22, 22, 22, 22, beijing)
var (
sec = t.Unix()
msec = t.UnixMilli()
usec = t.UnixMicro()
)
// 将秒级时间戳转为时间对象(第二个参数为不足1秒的纳秒数)
timeObj := time.Unix(sec, 22)
fmt.Println(timeObj) // 2022-02-22 22:22:22.000000022 +0800 CST
timeObj = time.UnixMilli(msec) // 毫秒级时间戳转为时间对象
fmt.Println(timeObj) // 2022-02-22 22:22:22 +0800 CST
timeObj = time.UnixMicro(usec) // 微秒级时间戳转为时间对象
fmt.Println(timeObj) // 2022-02-22 22:22:22 +0800 CST
time.Duration是time包定义的一个类型,它代表两个时间点之间经过的时间,以纳秒为单位。time.Duration表示一段时间间隔,可表示的最长时间段大约290年。
time 包中定义的时间间隔类型的常量如下:
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
例如:time.Duration表示1纳秒,time.Second表示1秒
now := time.Now()
later := now.Add(time.Hour) // 当前时间加1小时后的时间
fmt.Println(later)
func tickDemo() {
ticker := time.Tick(time.Second) //定义一个1秒间隔的定时器
for i := range ticker {
fmt.Println(i)//每秒都会执行的任务
}
}
边栏推荐
- Stringutils tool class
- 2022第六季完美童模陕西总决赛圆满落幕
- Class C design questions
- Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
- Live-Server使用
- Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
- USB (XV) 2022-04-14
- B_ QuRT_ User_ Guide(37)
- SAP HR family member information
- C simple question 2
猜你喜欢
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
Summary of SQL single table query 2020.7.27
SAP HR 社会工作经历 0023
进度播报|广州地铁七号线全线29台盾构机全部完成始发
0-1 knapsack problem
C simple question one
Dataguard 主备清理归档设置
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
二叉排序树【BST】——创建、查找、删除、输出
SAP 内存参数调优过程
随机推荐
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
Understand TCP's three handshakes and four waves with love
Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
MongoDB快速入门
postgres timestamp转人眼时间字符串或者毫秒值
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
Sequence of entity layer, Dao layer, service layer and controller layer
Dependency injection
SAP HR 社会工作经历 0023
C语言学习
Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
IDEA 2021.3. X cracking
648. Word replacement
SQL database execution problems
KeePass realizes automatic input of web pages
The file format and extension of XLS do not match
USB (XVI) 2022-04-28
Right click the idea file to create new. There is no solution to create new servlet
Extended tree (I) - graphic analysis and C language implementation
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)