本文共 4557 字,大约阅读时间需要 15 分钟。
golang的时间转换基本都围绕这 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 就是取的当前操作系统的时区。
通过特定的日期时间得到 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
将字符串转换到特定时区下的 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)
将字符串转换到当前系统时区下的 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"))
当前系统时区下的 Time 对象
t := time.Now()
将时间戳,纳秒转换成当前系统时区下的 Time 对象
t := time.Unix(1602604800, 0)
加,减时间,得到一个新的 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
// 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整数。
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/