当前位置:网站首页>Time and Duration and Epoch
Time and Duration and Epoch
2022-04-21 07:45:00 【Xiong Xiaohan】
current time
now := time.Now()
fmt.Println("now:", now)
now: 2022-04-16 10:50:52.283976 +0800 CST m=+0.000111228
Specify time
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
fmt.Println("then:", then)
then: 2009-11-17 20:34:58.651387237 +0000 UTC
The parameters are : year , month , Japan , when , branch , second , nanosecond , The time zone .
The time field
now := time.Now()
fmt.Println("now:", now)
fmt.Println("year:", now.Year())
fmt.Println("month:", now.Month())
fmt.Println("day:", now.Day())
fmt.Println("hour:", now.Hour())
fmt.Println("minute:", now.Minute())
fmt.Println("second:", now.Second())
fmt.Println("nanosecond:", now.Nanosecond())
fmt.Println("location:", now.Location())
fmt.Println("weekday:", now.Weekday())
now: 2022-04-16 11:10:21.76101 +0800 CST m=+0.000108970
year: 2022
month: April
day: 16
hour: 11
minute: 10
second: 21
nanosecond: 761010000
location: Local
weekday: Saturday
Time comparison
now := time.Now()
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
fmt.Println("then before now:", then.Before(now))
fmt.Println("then after now:", then.After(now))
fmt.Println("then equal now:", then.Equal(now))
then before now: true
then after now: false
then equal now: false
The time interval
now := time.Now()
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
diff := now.Sub(then)
fmt.Println("diff:", diff)
fmt.Println("hours:", diff.Hours())
fmt.Println("minutes:", diff.Minutes())
fmt.Println("seconds:", diff.Seconds())
fmt.Println("nanoseconds:", diff.Nanoseconds())
diff: 108774h59m46.516185763s
hours: 108774.99625449604
minutes: 6.526499775269763e+06
seconds: 3.9158998651618576e+08
nanoseconds: 391589986516185763
Time calculation
now := time.Now()
fmt.Println("now:", now)
fmt.Println("now add 3 hour", now.Add(3*time.Hour))
fmt.Println("now add -3 hour", now.Add(-3*time.Hour))
now: 2022-04-16 11:47:08.20477 +0800 CST m=+0.000109580
now add 3 hour 2022-04-16 14:47:08.20477 +0800 CST m=+10800.000109580
now add -3 hour 2022-04-16 08:47:08.20477 +0800 CST m=-10799.999890420
Time stamp
now := time.Now()
fmt.Println("now:", now)
fmt.Println("now unix:", now.Unix())
fmt.Println("now unix milli:", now.UnixMilli())
fmt.Println("now unix nano:", now.UnixNano())
now: 2022-04-16 13:18:19.23993 +0800 CST m=+0.000111504
now unix: 1650086299
now unix milli: 1650086299239
now unix nano: 1650086299239930000
Timestamp conversion
fmt.Println("time from unix:", time.Unix(1650080700, 0))
fmt.Println("time from unix nano:", time.Unix(0, 1650080700587122000))
time from unix: 2022-04-16 11:45:00 +0800 CST
time from unix nano: 2022-04-16 11:45:00.587122 +0800 CST
Time format
Here are the instructions for formatting :
// Year: “2006” “06”
// Month: “Jan” “January”
// Textual day of the week: “Mon” “Monday”
// Numeric day of the month: “2” “_2” “02”
// Numeric day of the year: “__2” “002”
// Hour: “15” “3” “03” (PM or AM)
// Minute: “4” “04”
// Second: “5” “05”
// AM/PM mark: “PM”
//
// Numeric time zone offsets format as follows:
// “-0700” ±hhmm
// “-07:00” ±hh:mm
// “-07” ±hh
// Replacing the sign in the format with a Z triggers
// the ISO 8601 behavior of printing Z instead of an
// offset for the UTC zone. Thus:
// “Z0700” Z or ±hhmm
// “Z07:00” Z or ±hh:mm
// “Z07” Z or ±hh
now := time.Now()
fmt.Println(now.Format(time.RFC3339))
fmt.Println(now.Format("3:04PM"))
fmt.Println(now.Format("Mon Jan _2 15:04:05 2006"))
fmt.Println(now.Format("2006-01-02T15:04:05.999999-07:00"))
2022-04-16T14:24:19+08:00
2:24PM
Sat Apr 16 14:24:19 2022
2022-04-16T14:24:19.73223+08:00
now := time.Now()
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
2022-04-16T14:31:59-00:00
Time string parsing
layout := "3 04 PM"
t, _ := time.Parse(layout, "8 41 PM")
fmt.Println(t)
0000-01-01 20:41:00 +0000 UTC
t, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
fmt.Println(t)
2012-11-01 22:08:41 +0000 +0000
When parsing fails ,error Will be returned to .
layout := "Mon Jan _2 15:04:05 2006"
_, e := time.Parse(layout, "8:41PM")
fmt.Println(e)
parsing time “8:41PM” as “Mon Jan _2 15:04:05 2006”: cannot parse “8:41PM” as “Mon”
版权声明
本文为[Xiong Xiaohan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210627175926.html
边栏推荐
猜你喜欢
随机推荐
laravel 打印sql
Number
window 系统丢失北京时区解决方案
php 文章关键字替换类
SQLServer的3种分页查询方式sql2000/2008/2012的
Defining “Disinformation“(定义“虚假信息”)
汇编语言——内存定位的方法
sqlmap post 注入方法
mysql底层的引擎选择,为什么索引结构选择B+树:(3.28-4.3)
服务器部署svn环境
2022-1-17至2022-1-30 迭代器模式
playwright,selenium,操作ifram元素
PLSQL14软件包下载、汉化、注册
leetcode 707.设计链表
论文阅读:Cached and Confused: Web Cache Deception in the wild
Leetcode 1423.可获得的最大点数(Maximum Points You Can Obtain from Cards)
Leetcode 1557.可以到达所有点的最少点数目(Minimum Number of Vertices to Reach All Nodes)
文件打包下载
PHP 对象转数组
Accumulation of DB2 related operation knowledge points and instance of DB2 connecting to remote database in Windows Environment









