一.导入包
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类型
时分秒
时间戳
- 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 {} 返回增加了给出的年份、月份和天 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|