package util import ( "fmt" "time" ) const ( LocationName = "Asia/Shanghai" LayoutTime = "2006-01-02 15:04:05" Layout = "2006-01-02" ) // TimeParseLocalUnix 获取当天零点的时间戳 // eg 2023-02-22 => 1676995200 func TimeParseLocalUnix(DayTime string) int64 { value := DayTime if len(DayTime) <= 11 { value = fmt.Sprintf("%s 00:00:00", DayTime) } loc, _ := time.LoadLocation("Local") theTime, _ := time.ParseInLocation(LayoutTime, value, loc) return theTime.Unix() } // ConvertParseLocalUnix 字符串转换当天时间戳 // eg 15:04:05 => 1676998245 func ConvertParseLocalUnix(timeParse string) (int64, error) { loc, err := time.LoadLocation("Local") if err != nil { return 0, err } value := fmt.Sprintf("%s %s", time.Now().Format(Layout), timeParse) theTime, err := time.ParseInLocation(LayoutTime, value, loc) if err != nil { return 0, err } return theTime.Unix(), nil } // GetMonthRemainDay 获取当前月还剩几天 func GetMonthRemainDay() int { now := time.Now() lastDayOfMonth := time.Date(now.Year(), now.Month()+1, 0, 23, 59, 59, 999999999, now.Location()) return int(lastDayOfMonth.Sub(now).Hours()/24) + 1 }