当前位置:网站首页>Go time package common functions
Go time package common functions
2022-07-07 23:44:00 【Cough, Hello, please give me more advice!】
now := time.Now() // Get the current time
fmt.Printf(" current time %v\n", now)
year := now.Year() // year
month := now.Month() // month
day := now.Day() // Japan
hour := now.Hour() // Hours
minute := now.Minute() // minute
second := now.Second() // second
secondsEastOfUTC := int((8 * time.Hour).Seconds())
beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
// fmt.Print(beijing.String())
// Beijing time. 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()
)
// Turn the second timestamp into a time object ( The second parameter is insufficient 1 Nanoseconds per second )
timeObj := time.Unix(sec, 22)
fmt.Println(timeObj) // 2022-02-22 22:22:22.000000022 +0800 CST
timeObj = time.UnixMilli(msec) // Millisecond timestamps are converted to time objects
fmt.Println(timeObj) // 2022-02-22 22:22:22 +0800 CST
timeObj = time.UnixMicro(usec) // Microsecond timestamps are converted to time objects
fmt.Println(timeObj) // 2022-02-22 22:22:22 +0800 CST
time.Duration yes time A type of package definition , It represents the time between two time points , In nanoseconds .time.Duration A time interval , The longest period that can be expressed is about 290 year .
time The constants of the interval type defined in the package are as follows :
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
for example :time.Duration Express 1 nanosecond ,time.Second Express 1 second
now := time.Now()
later := now.Add(time.Hour) // Current time plus 1 Hours later
fmt.Println(later)
func tickDemo() {
ticker := time.Tick(time.Second) // Define a 1 Second interval timer
for i := range ticker {
fmt.Println(i)// Tasks that are performed every second
}
}
边栏推荐
- Oracle statistics by time
- Enumeration, simulation, and sorting
- ASP. Net core middleware request processing pipeline
- Boost regex library source code compilation
- Oracle string sorting
- 507 field D - extraterrestrial relics
- [stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
- BSS 7230 航空内饰材料阻燃性能测试
- KeePass realizes automatic input of web pages
- Chisel tutorial - 01 Introduction to Scala
猜你喜欢
随机推荐
MySQL架构
Jisuan Ke - t3104
Svn relocation
C cat and dog
aws-aws help报错
[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
Chisel tutorial - 01 Introduction to Scala
May day d-light
Where are you going
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
【实验分享】通过Console口登录到Cisco设备
BSS 7230 航空内饰材料阻燃性能测试
HDU - 1260 Tickets(线性DP)
Fibonacci number of dynamic programming
C number of words, plus ¥, longest word, average value
The file format and extension of XLS do not match
SAP HR 劳动合同信息 0016
C - linear table
蓝桥ROS中使用fishros一键安装
SAP HR reward and punishment information export