You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
2 months ago
|
package internal
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// GetBetweenDates 根据开始日期和结束日期计算出时间段内所有日期
|
||
|
// 参数为日期格式,如:2020-01-01
|
||
|
func GetBetweenDates(sdate, edate string) []string {
|
||
|
d := []string{}
|
||
|
timeFormatTpl := "2006-01-02 15:04:05"
|
||
|
if len(timeFormatTpl) != len(sdate) {
|
||
|
timeFormatTpl = timeFormatTpl[0:len(sdate)]
|
||
|
}
|
||
|
date, err := time.Parse(timeFormatTpl, sdate)
|
||
|
if err != nil {
|
||
|
// 时间解析,异常
|
||
|
return d
|
||
|
}
|
||
|
date2, err := time.Parse(timeFormatTpl, edate)
|
||
|
if err != nil {
|
||
|
// 时间解析,异常
|
||
|
return d
|
||
|
}
|
||
|
if date2.Before(date) {
|
||
|
// 如果结束时间小于开始时间,异常
|
||
|
return d
|
||
|
}
|
||
|
// 输出日期格式固定
|
||
|
timeFormatTpl = "2006-01-02"
|
||
|
date2Str := date2.Format(timeFormatTpl)
|
||
|
d = append(d, date.Format(timeFormatTpl))
|
||
|
for {
|
||
|
date = date.AddDate(0, 0, 1)
|
||
|
dateStr := date.Format(timeFormatTpl)
|
||
|
d = append(d, dateStr)
|
||
|
if dateStr == date2Str {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
return d
|
||
|
}
|
||
|
|
||
|
func StringReplace(str string) string {
|
||
|
return strings.Replace(str, " ", "", -1)
|
||
|
}
|
||
|
|
||
|
func ConversionTime(beforeTime string) int64 {
|
||
|
afterTime, err := time.ParseInLocation("2006-01-02", beforeTime, time.Local)
|
||
|
if err != nil {
|
||
|
afterTime, err = time.ParseInLocation("2006-01-02", beforeTime, time.Local)
|
||
|
if err != nil {
|
||
|
afterTime, err = time.ParseInLocation("2006-01-02 15:04:05", beforeTime, time.Local)
|
||
|
}
|
||
|
}
|
||
|
return afterTime.Unix()
|
||
|
}
|