当前位置:网站首页>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
}
}
边栏推荐
猜你喜欢
保证接口数据安全的10种方案
AITM3.0005 烟雾毒性测试
Aitm3.0005 smoke toxicity test
MP4文件格式解析之结合实例分析
Live server usage
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
Markdown
Learn about scratch
Open source hardware small project: anxinco esp-c3f control ws2812
Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
随机推荐
Understand TCP's three handshakes and four waves with love
SQL 使用in关键字查询多个字段
SAP HR 社会工作经历 0023
Markdown
Oracle statistics by time
Flash encryption process and implementation of esp32
The for loop realizes 1-100 addition and eliminates the 4-digit tail number
ASP. Net query implementation
网上买基金安全么?
[untitled]
HDU - 1260 tickets (linear DP)
MySQL架构
Lm12 rolling heikin Ashi double K-line filter
Chisel tutorial - 01 Introduction to Scala
Jisuan Ke - t3104
List. How to achieve ascending and descending sort() 2020.8.6
webflux - webclient Connect reset by peer Error
【7.5】15. Sum of three numbers
Map operation execution process
Anxin vb01 offline voice module access intelligent curtain guidance