Skip to content

Instantly share code, notes, and snippets.

@paulyung541
Last active March 4, 2020 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulyung541/5e1d6cdc4ad413ea22ddd061c0dfd40a to your computer and use it in GitHub Desktop.
Save paulyung541/5e1d6cdc4ad413ea22ddd061c0dfd40a to your computer and use it in GitHub Desktop.
go时间相关操作
package main
// 自定义的时间字符串转 Time 对象
func String2Time() {
const longForm = "2006-01-02 15:04:05"
t, err := time.Parse(longForm, "1991-01-02 15:04:05")
if err != nil {
panic(err)
}
// t Time 的相关操作...
}
// Unix 时间戳转 Time 对象
func Unix2Time() {
// 第二个参数是纳秒,一般就置为0就好了
t := time.Unix(1580645504, 0)
fmt.Println(t)
}
// 相对目前时间的一天开始和结束的时间戳
func GetDayUnix(days int) (int64, int64) {
now := time.Now()
year, month, day := now.Date()
start := time.Date(year, month, day+days, 0, 0, 0, 0, now.Location()).Unix()
end := time.Date(year, month, day+days, 23, 59, 59, 0, now.Location()).Unix()
return start, end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment