Golang-日期格式转换

最近遇到了一些日期类型转换的问题,字符串转date类型,时间的加减以及时分秒的获取等等…参考官网这里做一下总结。

在time包里主要有以下几个数据类型:time.Time,time.Month,time.Weekday,time.Duration,time.Location

time.Time

time.Time 代表一个纳秒精度的时间点.

type Time struct {
sec int64 // 从1年1月1日 00:00:00 UTC 至今过去的秒数
nsec int32 // 最近一秒到下一秒过去的纳秒数
loc *Location // 时区
}

time.Month

type Month int

// 相关常量
const (
January Month = 1 + iota
February
March
April
May
June
July
August
September
October
November
December
)

time.Weekday

type Weekday int

// 相关常量
const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)

time.Duration

time.Duration 类型代表两个时间点之间经过的纳秒数,可表示的最长时间段约为290年。

type Duration int64

// 相关常量
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)

time.Location

Location代表一个地点,以及该地点所在的时区信息。北京时间可以使用Asia/Shanghai

type Location struct {
name string
zone []zone
tx []zoneTrans
cacheStart int64
cacheEnd int64
cacheZone *zone
}

// 预定义时区变量
var UTC *Location = &utcLoc
var Local *Location = &localLoc

获取时间

  • func Now() Time {} // 当前本地时间

  • func Unix(sec int64, nsec int64) Time {} // 根据时间戳返回本地时间

  • func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {} // 返回指定时间

  • func (t Time) UTC() Time {} // 获取指定时间在UTC 时区的时间表示

  • func (t Time) Local() Time {} // 以本地时区表示

  • func (t Time) In(loc *Location) Time {} // 时间在指定时区的表示

  • func (t Time) Format(layout string) string {} // 按指定格式显示时间

// 当前本地时间
t = time.Now()
fmt.Println("'time.Now': ", t)

// 根据时间戳返回本地时间
t_by_unix := time.Unix(1487780010, 0)
fmt.Println("'time.Unix': ", t_by_unix)

// 返回指定时间
t_by_date := time.Date(2017, time.Month(2), 23, 1, 30, 30, 0, l)
fmt.Println("'time.Date': ", t_by_date)

// 获取指定时间在UTC 时区的时间表示
t_by_utc := t.UTC()
fmt.Println("'t.UTC': ", t_by_utc)

// 获取本地时间表示
t_by_local := t.Local()
fmt.Println("'t.Local': ", t_by_local)

// 时间在指定时区的表示
t_in := t.In(time.UTC)
fmt.Println("'t.In': ", t_in)

// Format
fmt.Println("t.Format", t.Format(time.RFC3339))

获取日期

  • func (t Time) Date() (year int, month Month, day int) {} // 返回时间的日期信息

  • func (t Time) Year() int {} // 返回年

  • func (t Time) Month() Month {} // 月

  • func (t Time) Day() int {} // 日

  • func (t Time) Weekday() Weekday {} // 星期

  • func (t Time) ISOWeek() (year, week int) {} // 返回年,星期范围编号

  • func (t Time) Clock() (hour, min, sec int) {} // 返回时间的时分秒

  • func (t Time) Hour() int {} // 返回小时

  • func (t Time) Minute() int {} // 分钟

  • func (t Time) Second() int {} // 秒

  • func (t Time) Nanosecond() int {} // 纳秒

  • func (t Time) YearDay() int {} // 一年中对应的天

  • func (t Time) Location() *Location {} // 时间的时区

  • func (t Time) Zone() (name string, offset int) {} // 时间所在时区的规范名和想对UTC 时间偏移量

  • func (t Time) Unix() int64 {} // 时间转为时间戳

  • func (t Time) UnixNano() int64 {} // 时间转为时间戳(纳秒)

// 返回时间的日期信息
year, month, day := t.Date()
fmt.Println("'t.Date': ", year, month, day)

// 星期
week := t.Weekday()
fmt.Println("'t.Weekday': ", week)

// 返回年,星期范围编号
year, week_int := t.ISOWeek()
fmt.Println("'t.ISOWeek': ", year, week_int)

// 返回时间的时分秒
hour, min, sec := t.Clock()
fmt.Println("'t.Clock': ", hour, min, sec)

暂时只用到了这些,后面还有些序列化以及比较的方法,以后用到再做补充~

To be continued!!!