当前位置:网站首页>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)//每秒都会执行的任务
}
}
边栏推荐
- Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
- Dependency injection
- SAP memory parameter tuning process
- Anxin vb01 offline voice module access intelligent curtain guidance
- Live server usage
- Come on, brother
- New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
- Navicat connects Oracle
- 包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
- SAP HR labor contract information 0016
猜你喜欢
postgis学习
ASP. Net core middleware request processing pipeline
平衡二叉樹【AVL樹】——插入、删除
ping报错:未知的名称或服务
[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
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
C inheritance and interface design polymorphism
ESP at installation esp8266 and esp32 versions
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
Open source hardware small project: anxinco esp-c3f control ws2812
随机推荐
Dependency injection 2 advantage lifecycle
激光slam学习(2D/3D、偏实践)
B_QuRT_User_Guide(38)
B_ QuRT_ User_ Guide(39)
SAP HR 社会工作经历 0023
Get started with mongodb
SLAM面试总结
Happy gathering time
Where are you going
0-1背包问题
USB (XVIII) 2022-04-17
B_QuRT_User_Guide(36)
C method question 2
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
Learn about scratch
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
Design and implementation of spark offline development framework
Take you hand in hand to build Eureka server with idea
MySQL Index Optimization Practice II
Oracle string sorting