博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang中的时间之间的转换
阅读量:3987 次
发布时间:2019-05-24

本文共 4557 字,大约阅读时间需要 15 分钟。

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

time 包下得到 Time 对象的方法

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Timefunc Now() Timefunc 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 environmentvariable to find the time zone to use. No TZ meansuse 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// 获取当月最后一天的时间:也就是下个月的第一天减1t2 := t1.AddDate(0, 1, -1)fmt.Println(t2) // 2021-02-28 00:00:00 +0800 CSTfmt.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.006844801fmt.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.004893001fmt.Println(t1.String()) // 2020-10-14 11:16:51.0480935 +0800 CST m=+1.004893001

转载地址:http://qcaui.baihongyu.com/

你可能感兴趣的文章
电平触发方式和边沿触发的区别
查看>>
中断函数中不能调用ioremap()!!!!!!!
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
EFM32TG222F32连接JLink V8问题
查看>>
Keil问题
查看>>
移植QT
查看>>
交叉编译qt-everywhere-4.8.4
查看>>
tslib qt4 segmentation fault
查看>>
QT隐藏mouse
查看>>
求最短路径
查看>>
找到一个数组后面第一个大的数
查看>>
找到一个链表中倒数第k个数
查看>>
两个队列实现一个栈
查看>>
用两个栈实现一个带getMin()方法的新型栈
查看>>
头条搜索部门后台开发实习生面经
查看>>
java 线程池
查看>>
设计模式之单例模式
查看>>
自己写的String类能够被加载吗?
查看>>
java让主线程等待所有子线程执行完应该怎么做
查看>>