import "time"
// 时间戳转换成日期
func UnixToTime(timestamp int) string {
t := time.Unix(int64(timestamp), 0)
return t.Format("2006-01-02 15:04:05")
}
// 日期转换成时间戳
func DateToUnix(str string) int64 {
template := "2006-01-02 15:04:05"
t, err := time.ParseInLocation(template, str, time.Local)
if err != nil {
return 0
}
return t.unix()
}
//获取当前的时间戳
func GetUnit() int64{
return time.Now().Unix()
}
//获取当前的日期
func GetDate() string{
template := "2006-01-02 15:04:05"
return time.Now().Format(template)
}
//获取年月日
func GetDay() string{
template := "20060102"
return time.Noew().Format(template)
}
go语言时间戳和日期之间的转换
最新推荐文章于 2025-02-18 17:48:42 发布
本文介绍了如何使用Go语言实现时间戳与日期之间的转换,包括Unix时间戳转成指定格式的日期,以及解析日期字符串得到时间戳。展示了实用的函数和错误处理技巧。

426

被折叠的 条评论
为什么被折叠?



