mh_server/lib/utils/time.go
2021-06-30 10:12:05 +08:00

55 lines
1.3 KiB
Go

package utils
import (
"fmt"
"time"
)
func GetTodayBegin() time.Time {
year, month, day := time.Now().Date()
return time.Date(year, month, day, 0, 0, 0, 0, time.Now().Location())
}
func GetYesterdayBegin() time.Time {
todayUnix := GetTodayBegin().Unix()
yesterdayUnix := todayUnix - 3600*24
return time.Unix(yesterdayUnix, 0)
}
func GetThisMonthBegin() time.Time {
year, month, _ := time.Now().Date()
return time.Date(year, month, 0, 0, 0, 0, 0, time.Now().Location())
}
func GetTodayDate() string {
year := time.Now().Format("2006")
month := time.Now().Format("01")
day := time.Now().Format("02")
return fmt.Sprintf("%s-%s-%s", year, month, day)
}
func DatetimeTransformationTimestamp(datetime string) time.Time {
//日期转化为时间戳
timeLayout := "2006-01-02 15:04:05" //转化所需模板
loc, _ := time.LoadLocation("Local") //获取时区
tmp, _ := time.ParseInLocation(timeLayout, datetime, loc)
tmp.UnixNano()
return tmp
}
func TimeToDate(dateTime time.Time) string {
year := dateTime.Format("2006")
month := dateTime.Format("01")
day := dateTime.Format("02")
return fmt.Sprintf("%s-%s-%s", year, month, day)
}
func Timestamp2Date(timestamp int64) string {
timeLayout := "2006-01-02 15:04:05"
datetime := time.Unix(timestamp, 0).Format(timeLayout)
return datetime
}