当前位置:网站首页>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)//每秒都会执行的任务
}
}
边栏推荐
- JNI uses asan to check memory leaks
- How to login and enable synchronization function in Google browser
- SQL database execution problems
- USB (XVII) 2022-04-15
- Display the server hard disk image to the browser through Servlet
- HDU 4747 mex "recommended collection"
- B_QuRT_User_Guide(36)
- B_ QuRT_ User_ Guide(37)
- 网上买基金安全么?
- SAP HR 家庭成员信息
猜你喜欢
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."
B_ QuRT_ User_ Guide(37)
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
平衡二叉樹【AVL樹】——插入、删除
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
ESP at installation esp8266 and esp32 versions
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
C # exchange number, judge to pass the exam
List. How to achieve ascending and descending sort() 2020.8.6
Get started with mongodb
随机推荐
Understand TCP's three handshakes and four waves with love
Three questions TDM
Design and implementation of spark offline development framework
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
Map operation execution process
MongoDB快速入门
Open source hardware small project: anxinco esp-c3f control ws2812
B_QuRT_User_Guide(39)
【LeetCode】20、有效的括号
通达信买基金安全吗?
Summary of SQL single table query 2020.7.27
Summary of common methods of object class (September 14, 2020)
USB (XVI) 2022-04-28
[summary] some panels and videos seen
UE4_ Ue5 panoramic camera
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
Idea automatically generates serialVersionUID
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行