文章介紹
本文我們來介紹一下go語言內建的time包,以例項的方式來介紹time包中常用的方法
time物件
type Time struct {
wall uint64
ext int64
loc *Location
}
Methods:
String() string
Format(layout string) string
AppendFormat(b []byte, layout string) []byte
nsec() int32
sec() int64
unixSec() int64
addSec(d int64)
setLoc(loc *time.Location)
stripMono()
setMono(m int64)
mono() int64
After(u time.Time) bool
Before(u time.Time) bool
Equal(u time.Time) bool
IsZero() bool
abs() uint64
locabs() (name string, offset int, abs uint64)
Date() (year int, month time.Month, day int)
Year() int
Month() time.Month
Day() int
Weekday() time.Weekday
ISOWeek() (year int, week int)
Clock() (hour int, min int, sec int)
Hour() int
Minute() int
Second() int
Nanosecond() int
YearDay() int
Add(d time.Duration) time.Time
Sub(u time.Time) time.Duration
AddDate(years int, months int, days int) time.Time
date(full bool) (year int, month time.Month, day int, yday int)
UTC() time.Time
Local() time.Time
In(loc *time.Location) time.Time
Location() *time.Location
Zone() (name string, offset int)
Unix() int64
UnixNano() int64
MarshalBinary() ([]byte, error)
UnmarshalBinary(data []byte) error
GobEncode() ([]byte, error)
GobDecode(data []byte) error
MarshalJSON() ([]byte, error)
UnmarshalJSON(data []byte) error
MarshalText() ([]byte, error)
UnmarshalText(data []byte) error
Truncate(d time.Duration) time.Time
Round(d time.Duration) time.Time
time包方法介紹
time.Now()獲取當前時間
func Now() Time
例項:
package main
import (
"fmt"
"time"
)
func main() {
//獲取當前時間
t := time.Now()
fmt.Println(t)
}
輸出:
2022-07-28 22:01:20.596934 +0800 CST m=+0.000081427
+0800
指時區-東八區
t.Format()格式化
func (t Time) Format(layout string) string
time物件提供了Format()方法,可以將時間格式化
例項:
package main
import (
"fmt"
"time"
)
func main() {
//獲取當前時間
t := time.Now()
fmt.Println(t)
//時間格式化
ts := t.Format("2006-01-02 15:04:05") //go誕生時間123456
fmt.Println(ts)
}
輸出:
2022-07-28 22:03:57.386954 +0800 CST m=+0.000098870
2022-07-28 22:03:57
time.LoadLocation()獲取相應時區時間
func LoadLocation(name string) (*Location, error)
例項:
package main
import (
"fmt"
"log"
"time"
)
func main() {
//獲取當前時間
t := time.Now()
fmt.Println(t)
//時區
loc, err := time.LoadLocation("America/New_York")
if err != nil {
log.Panicln("轉換時區失敗")
}
fmt.Println(t.In(loc)) //這裡需要配合t.In()
}
輸出:
2022-07-28 22:11:34.610774 +0800 CST m=+0.000089252
2022-07-28 10:11:34.610774 -0400 EDT
獲取年月日/時分秒
package main
import (
"fmt"
"time"
)
func main() {
//獲取當前時間
t := time.Now()
fmt.Println(t)
//年月日
fmt.Println(t.Year(), t.Month(), t.Day())
//時分秒
fmt.Println(t.Hour(), t.Minute(), t.Second())
}
輸出:
2022-07-28 22:15:06.056301 +0800 CST m=+0.000086151
2022 July 28
22 15 6
t.Parse()字串轉Time型別
func Parse(layout string, value string) (Time, error)
例項:
//獲取當前時間
t := time.Now()
fmt.Println(t)
//將字串轉換為time型別
layout := "2006-01-02 15:04:05"
ts := "2022-07-28 23:28:40"
t1, err := time.Parse(layout, ts)
if err != nil {
panic(err)
}
fmt.Println(t1)
輸出:
2022-07-28 22:55:00.753234 +0800 CST m=+0.000100299
2022-07-28 23:28:40 +0000 UTC
時間運算
func (t Time) Add(d Duration) Time
當前時間t+引數d
例項:
//獲取當前時間
t := time.Now()
fmt.Println(t)
fmt.Println(t.Add(1 * time.Hour)) //當前時間+1小時
fmt.Println(t.AddDate(0, 1, 1)) //當前時間+年月日
輸出:
2022-07-28 23:04:08.400064 +0800 CST m=+0.000082336
2022-07-29 00:04:08.400064 +0800 CST m=+3600.000082336
2022-08-29 23:04:08.400064 +0800 CST
func (t Time) Sub(u Time) Duration
當前時間t - 時間u
例項:
//獲取當前時間
t := time.Now()
fmt.Println(t)
//時間差值
newAdd := t.Add(1 * time.Hour)
fmt.Println(t.Sub(newAdd)) //當前時間-引數
輸出:
2022-07-28 23:04:08.400064 +0800 CST m=+0.000082336
-1h0m0s
時間比較
t := time.Now()
fmt.Println(t)
//時間比較
newAdd := t.Add(1 * time.Hour)
fmt.Println(t.Equal(newAdd)) // = t
fmt.Println(t.After(newAdd)) // > t
fmt.Println(t.Before(newAdd)) // < t
輸出:
2022-07-28 23:15:39.781098 +0800 CST m=+0.000088697
false
false
true
t.Unix()時間戳
func (t Time) Unix() int64
返回int64,使用時間比較可以直接使用時間戳比較
時間戳例項
t := time.Now()
fmt.Println(t)
//獲取時間戳
fmt.Println(t.Unix()) //秒
fmt.Println(t.UnixNano()) //納秒
輸出:
2022-07-28 23:21:07.827182 +0800 CST m=+0.000126192
1659021667
1659021667827182000
本作品採用《CC 協議》,轉載必須註明作者和本文連結