golang中的时间之间的转换 - Go语言中文社区

golang中的时间之间的转换


golang的时间转换基本都围绕这 Time 类型,所以,首先要得到 Time 对象,再说去转换成别的格式。

time 包下得到 Time 对象的方法

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
func Now() Time
func Parse(layout, value string) (Time, error)
func ParseInLocation(layout, value string, loc *Location) (Time, error)
func Unix(sec int64, nsec int64) Time

关于时区
国际时区:time.UTC

UTC represents Universal Coordinated Time (UTC).

本地时区:time.Local

Local represents the system's local time zone.
On Unix systems, Local consults the TZ environment
variable to find the time zone to use. No TZ means
use the system default /etc/localtime.
TZ="" means use UTC.
TZ="foo" means use file foo in the system timezone directory.

time.Local 就是取的当前操作系统的时区。

Date

通过特定的日期时间得到 Time 对象

t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

time.Date()函数非常强大,例如,我想知道某个月有多少天,查了一下,没有提供这个函数,但是可以通过time.Date()来实现。

date := "2021-02"
t, _ := time.ParseInLocation("2006-01", date, time.Local)

// 获取到年和月
y, m, _ := t.Date()

// 获取当月第一天的事件
t1 := time.Date(y, m, 1, 0,0,0,0,time.Local)
fmt.Println(t1) // 2021-02-01 00:00:00 +0800 CST

// 获取当月最后一天的时间:也就是下个月的第一天减1
t2 := t1.AddDate(0, 1, -1)
fmt.Println(t2) // 2021-02-28 00:00:00 +0800 CST

fmt.Println(1 + t2.Sub(t1).Hours() / 24) // 28

ParseInLocation

将字符串转换到特定时区下的 Time 对象

t, err := time.ParseInLocation("2006-01-02", "2020-10-14", time.Local)
t, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-10-14 00:00:00", time.Local)

Parse

将字符串转换到当前系统时区下的 Time 对象,为了安全起见建议使用 ParseInLocation。

t, err := time.Parse("2006-01-02", "2020-10-14")
t, err := time.Parse("2006-01-02 15:04:05", "2020-10-14 00:00:00")

今天零点的时间

t, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))

Now

当前系统时区下的 Time 对象

t := time.Now()

Unix

将时间戳,纳秒转换成当前系统时区下的 Time 对象

t := time.Unix(1602604800, 0)

Time 对象提供的方法

加,减时间,得到一个新的 Time 对象
	func (t Time) Add(d Duration) Time 
	func (t Time) AddDate(years int, months int, days int) Time
	func (t Time) Sub(u Time) Duration ------ t - u = Duration
	
	t1 := time.Now().Add(-time.Minute * 3)
	t2 := time.Now().Add(time.Minute * 3)

时间比较
	func (t Time) After(u Time) bool
	func (t Time) Before(u Time) bool
	func (t Time) Equal(u Time) bool

获取年月日时分秒周
	func (t Time) Date() (year int, month Month, day int)
	func (t Time) Clock() (hour, min, sec int)
	func (t Time) Year() int 
	func (t Time) Day() int 		------ day of the month
	func (t Time) Hour() int 		------ [0, 23]
	func (t Time) Minute() int 		------ [0, 59]
	func (t Time) Month() Month 	------ month of the year
	func (t Time) Second() int 		------ [0, 59]
	func (t Time) YearDay() int 	------ day of the year,[1,365][1,366]
	func (t Time) Weekday() Weekday ------ day of the week
	func (t Time) Nanosecond() int 	------ nanosecond offset within the second

输出与格式化
	func (t Time) Format(layout string) string
	func (t Time) String() string
	输出 2020-10-14 10:23:28.1632997 +0800 CST m=-179.994143799

	func (t Time) MarshalBinary() ([]byte, error)
	func (t Time) MarshalJSON() ([]byte, error)
	func (t Time) MarshalText() ([]byte, error)
	func (t *Time) UnmarshalBinary(data []byte) error
	func (t *Time) UnmarshalJSON(data []byte) error
	func (t *Time) UnmarshalText(data []byte) error

	func (t Time) Round(d Duration) Time
	func (t Time) Truncate(d Duration) Time

	func (t Time) AppendFormat(b []byte, layout string) []byte

时区
	func (t Time) In(loc *Location) Time	------ 转换到特定时区
	func (t Time) Local() Time				------ 转换到当前时区
	func (t Time) UTC() Time				------ 转换到国际时区
	func (t Time) Location() *Location		------ 返回 t 的时区
	func (t Time) Zone() (name string, offset int) ------ 返回 t 时区的名称以及偏移的秒数
		fmt.Println(time.Now().Zone()) // CST 28800
		CST可视为美国、澳大利亚、古巴或中国的标准时间
		28800 为东八区的偏移量 8*3600

时间戳
	func (t Time) Unix() int64
	func (t Time) UnixNano() int64

其他
	func (t *Time) GobDecode(data []byte) error
	func (t Time) GobEncode() ([]byte, error)
	func (t Time) ISOWeek() (year, week int)
	func (t Time) IsZero() bool

关于 Duration 类型

// A Duration represents the elapsed time between two instants
// as an int64 nanosecond count. The representation limits the
// largest representable duration to approximately 290 years.

type Duration int64

是以纳秒记的一个Int64整数。
秒,毫秒,微秒,纳秒
所以 1秒 = 1000,000,000 纳秒

t := time.Now()
t1 := t.Add(time.Duration(1000000000)) // 1 秒
fmt.Println(t.String())  // 2020-10-14 11:05:49.8105513 +0800 CST m=+0.006844801
fmt.Println(t1.String()) // 2020-10-14 11:05:50.8105513 +0800 CST m=+1.006844801

由于 Duration 其实就是 int64,所以也可以这样写

t := time.Now()
t1 := t.Add(1000000000) // 1 秒
fmt.Println(t.String())  // 2020-10-14 11:16:50.0480935 +0800 CST m=+0.004893001
fmt.Println(t1.String()) // 2020-10-14 11:16:51.0480935 +0800 CST m=+1.004893001
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/raoxiaoya/article/details/109068177
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-05 01:08:03
  • 阅读 ( 423 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢