当前位置:网站首页>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)//每秒都会执行的任务
}
}
边栏推荐
- C inheritance and interface design polymorphism
- USB (XVIII) 2022-04-17
- UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
- Take you hand in hand to build feign with idea
- Fibonacci number of dynamic programming
- Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
- One of the anti climbing methods
- HDU 4747 mex "recommended collection"
- postgres timestamp转人眼时间字符串或者毫秒值
- 【7.4】25. Turn over the linked list in groups of K
猜你喜欢
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
C method question 1
Ora-02437 failed to verify the primary key violation
Dataguard 主备清理归档设置
SAP memory parameter tuning process
B / Qurt Utilisateur Guide (36)
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
Open source hardware small project: anxinco esp-c3f control ws2812
First week of July
随机推荐
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
Class C design questions
【7.5】15. Sum of three numbers
Dataguard 主备清理归档设置
Anti climbing means cracking the second
Get started with mongodb
SAP HR labor contract information 0016
B_QuRT_User_Guide(39)
MySQL Index Optimization Practice II
FPGA basics catalog
IDEA 2021.3. X cracking
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
C method question 1
Explain
Open source hardware small project: anxinco esp-c3f control ws2812
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
Extract the file name under the folder under win
C number of words, plus ¥, longest word, average value