获取时间
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
now := time.Now() //获取当前时间
p(now)
then := time.Date(2017, 10, 14, 14, 43, 0, 0, time.UTC) //创建时间
p(then)
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
p(then.Weekday())
//比较两个时间前后
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
diff := now.Sub(then) //两个时间差值
p(diff)
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
p(then.Add(diff))
p(then.Add(-diff))
}
//输出
2017-10-14 22:44:41.2590861 +0800 CST
2017-10-14 14:43:00 +0000 UTC
2017
October
14
14
43
0
0
UTC
Saturday
true
false
false
1m41.2590861s
0.028127523916666668
1.687651435
101.2590861
101259086100
2017-10-14 14:44:41.2590861 +0000 UTC
2017-10-14 14:41:18.7409139 +0000 UTC
获取unix时间至今秒数
// A common requirement in programs is getting the number
// of seconds, milliseconds, or nanoseconds since the
// [Unix epoch](http://en.wikipedia.org/wiki/Unix_time).
// Here's how to do it in Go.
package main
import "fmt"
import "time"
func main() {
// Use `time.Now` with `Unix` or `UnixNano` to get
// elapsed time since the Unix epoch in seconds or
// nanoseconds, respectively.
now := time.Now()
secs := now.Unix()
nanos := now.UnixNano()
fmt.Println(now)
// Note that there is no `UnixMillis`, so to get the
// milliseconds since epoch you'll need to manually
// divide from nanoseconds.
millis := nanos / 1000000
fmt.Println(secs)
fmt.Println(millis)
fmt.Println(nanos)
// You can also convert integer seconds or nanoseconds
// since the epoch into the corresponding `time`.
fmt.Println(time.Unix(secs, 0))
fmt.Println(time.Unix(0, nanos))
}
//输出
2017-10-14 23:13:07.415913 +0800 CST
1507993987
1507993987415
1507993987415913000
2017-10-14 23:13:07 +0800 CST
2017-10-14 23:13:07.415913 +0800 CST
时间格式化
// Go supports time formatting and parsing via
// pattern-based layouts.
package main
import "fmt"
import "time"
func main() {
p := fmt.Println
// Here's a basic example of formatting a time
// according to RFC3339, using the corresponding layout
// constant.
t := time.Now()
p(t.Format(time.RFC3339))
// Time parsing uses the same layout values as `Format`.
t1, e := time.Parse(
time.RFC3339,
"2012-11-01T22:08:41+00:00")
p(t1)
// `Format` and `Parse` use example-based layouts. Usually
// you'll use a constant from `time` for these layouts, but
// you can also supply custom layouts. Layouts must use the
// reference time `Mon Jan 2 15:04:05 MST 2006` to show the
// pattern with which to format/parse a given time/string.
// The example time must be exactly as shown: the year 2006,
// 15 for the hour, Monday for the day of the week, etc.
p(t.Format("3:04PM"))
p(t.Format("Mon Jan _2 15:04:05 2006"))
p(t.Format("2006-01-02T15:04:05.999999-07:00"))
form := "3 04 PM"
t2, e := time.Parse(form, "8 41 PM")
p(t2)
// For purely numeric representations you can also
// use standard string formatting with the extracted
// components of the time value.
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
// `Parse` will return an error on malformed input
// explaining the parsing problem.
ansic := "Mon Jan _2 15:04:05 2006"
_, e = time.Parse(ansic, "8:41PM")
p(e)
}
//输出
2017-10-14T23:17:44+08:00
2012-11-01 22:08:41 +0000 +0000
11:17PM
Sat Oct 14 23:17:44 2017
2017-10-14T23:17:44.022096+08:00
0000-01-01 20:41:00 +0000 UTC
2017-10-14T23:17:44-00:00
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": cannot parse "8:41PM" as "Mon"
参考文章:
登陆发表评论