Go 时间相关

>获取当前时间:

t := time.Now()

 

>获取当天开始、结束时间:

tm1 := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
tm2 := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())

 

>获取相应时间戳:

timestamp := tm1.Unix()

 

>时间戳转为字符串格式:

timestr := time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")  //必须用2006-01-02 15:04:05这个时间,可以换用其他格式

 

 >字符串格式转为指定格式的时间:

t, _ := time.Parse("2006-01-02 15:04:05", timestr)  //第一个参数用于指定格式

  如果需要时区,则用:ParseInLocation("2006-01-02 15:04:05", timestr, loc)

 

posted @ 2017-06-28 10:23  哪来的查克拉  阅读(205)  评论(0编辑  收藏  举报