当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > Go-内置time包

Go-内置time包

2019年11月20日  | 移动技术网IT编程  | 我要评论

一.导入包

import "time"

二.转换成time对象

  • 获取当前时间:time. now ()
  • 自定义时间:time. date(year int, month month, day, hour, min, sec, nsec int, loc *location)

三.time对象的方法

年月日周

  • date() : 返回时间的日期信息 返回值int类型
  • year() : 返回年 返回值int类型
  • month () : 返回月 返回值month类型
  • day () : 返回日 返回值int类型
  • yearday() : 一年中对应的天 返回值int类型

年,星期范围编号

  • isoweek () : 返回年,星期范围编号int类型

时分秒

  • clock () : 返回 时分秒 返回值int类型

  • hour () : 返回时返回值int类型

  • minute () : 返回分 返回值int类型

  • second () : 返回秒 返回值int类型

  • nanosecond () : 返回纳秒 返回值int类型

时间戳

  • unix() :时间戳 返回值 int64
  • unixnano() :时间戳(纳秒) 返回值 int64

时区

  • location ():时区返回值 *location
  • zone() :时区偏差 返回值string+int

四.时间序列化和返序列化以及比较与计算

参考https://www.jianshu.com/p/9d5636d34f17

列化和返序列化

func (t time) marshalbinary() ([]byte, error) {} // 时间序列化

func (t time) unmarshalbinary(data []byte) error {} // 反序列化

func (t time) marshaljson() ([]byte, error) {} // 时间序列化

func (t time) marshaltext() ([]byte, error) {} // 时间序列化

func (t time) gobencode() ([]byte, error) {} // 时间序列化

func (t time) gobdecode() ([]byte, error) {} // 时间序列化

比较与计算

func (t time) iszero() bool {} // 是否是零时时间

func (t time) after(u time) bool {} // 时间在u 之前

func (t time) before(u time) bool {} // 时间在u 之后

func (t time) equal(u time) bool {} // 时间与u 相同

func (t time) add(d duration) time {} // 返回t +d 的时间点

func (t time) sub(u time) duration {} // 返回 t-u

func (t time) adddate(years int, months int, days int) time {} 返回增加了给出的年份、月份和天

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网