|
@@ -7,7 +7,6 @@ import (
|
|
|
"encoding/base64"
|
|
|
"encoding/hex"
|
|
|
"encoding/json"
|
|
|
- "errors"
|
|
|
"fmt"
|
|
|
"image"
|
|
|
"image/png"
|
|
@@ -722,96 +721,6 @@ func GetNowYearLastDay() time.Time {
|
|
|
return nowYearLastDay
|
|
|
}
|
|
|
|
|
|
-// CalculationDate 计算两个日期之间相差n年m月y天
|
|
|
-func CalculationDate(startDate, endDate time.Time) (beetweenDay string, err error) {
|
|
|
- //startDate := time.Date(2021, 3, 28, 0, 0, 0, 0, time.Now().Location())
|
|
|
- //endDate := time.Date(2022, 3, 31, 0, 0, 0, 0, time.Now().Location())
|
|
|
- numYear := endDate.Year() - startDate.Year()
|
|
|
-
|
|
|
- numMonth := int(endDate.Month()) - int(startDate.Month())
|
|
|
-
|
|
|
- numDay := 0
|
|
|
- //获取截止月的总天数
|
|
|
- endDateDays := getMonthDay(endDate.Year(), int(endDate.Month()))
|
|
|
-
|
|
|
- //获取截止月的前一个月
|
|
|
- endDatePrevMonthDate := endDate.AddDate(0, -1, 0)
|
|
|
- //获取截止日期的上一个月的总天数
|
|
|
- endDatePrevMonthDays := getMonthDay(endDatePrevMonthDate.Year(), int(endDatePrevMonthDate.Month()))
|
|
|
- //获取开始日期的的月份总天数
|
|
|
- startDateMonthDays := getMonthDay(startDate.Year(), int(startDate.Month()))
|
|
|
-
|
|
|
- //判断,截止月是否完全被选中,如果相等,那么代表截止月份全部天数被选择
|
|
|
- if endDate.Day() == endDateDays {
|
|
|
- numDay = startDateMonthDays - startDate.Day() + 1
|
|
|
-
|
|
|
- //如果剩余天数正好与开始日期的天数是一致的,那么月份加1
|
|
|
- if numDay == startDateMonthDays {
|
|
|
- numMonth++
|
|
|
- numDay = 0
|
|
|
- //超过月份了,那么年份加1
|
|
|
- if numMonth == 12 {
|
|
|
- numYear++
|
|
|
- numMonth = 0
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- numDay = endDate.Day() - startDate.Day() + 1
|
|
|
- }
|
|
|
-
|
|
|
- //天数小于0,那么向月份借一位
|
|
|
- if numDay < 0 {
|
|
|
- //向上一个月借一个月的天数
|
|
|
- numDay += endDatePrevMonthDays
|
|
|
-
|
|
|
- //总月份减去一个月
|
|
|
- numMonth = numMonth - 1
|
|
|
- }
|
|
|
-
|
|
|
- //月份小于0,那么向年份借一位
|
|
|
- if numMonth < 0 {
|
|
|
- //向上一个年借12个月
|
|
|
- numMonth += 12
|
|
|
-
|
|
|
- //总年份减去一年
|
|
|
- numYear = numYear - 1
|
|
|
- }
|
|
|
- if numYear < 0 {
|
|
|
- err = errors.New("日期异常")
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if numYear > 0 {
|
|
|
- beetweenDay += fmt.Sprint(numYear, "年")
|
|
|
- }
|
|
|
- if numMonth > 0 {
|
|
|
- beetweenDay += fmt.Sprint(numMonth, "个月")
|
|
|
- }
|
|
|
- if numDay > 0 {
|
|
|
- beetweenDay += fmt.Sprint(numDay, "天")
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-// getMonthDay 获取某年某月有多少天
|
|
|
-func getMonthDay(year, month int) (days int) {
|
|
|
- if month != 2 {
|
|
|
- if month == 4 || month == 6 || month == 9 || month == 11 {
|
|
|
- days = 30
|
|
|
-
|
|
|
- } else {
|
|
|
- days = 31
|
|
|
- }
|
|
|
- } else {
|
|
|
- if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
|
|
|
- days = 29
|
|
|
- } else {
|
|
|
- days = 28
|
|
|
- }
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
// SubStr 截取字符串(中文)
|
|
|
func SubStr(str string, subLen int) string {
|
|
|
strRune := []rune(str)
|
|
@@ -880,3 +789,18 @@ func GetOrmInReplace(num int) string {
|
|
|
}
|
|
|
return strings.Join(template, ",")
|
|
|
}
|
|
|
+
|
|
|
+func GetDaysBetween2Date(format, date1Str, date2Str string) (int, error) {
|
|
|
+ // 将字符串转化为Time格式
|
|
|
+ date1, err := time.ParseInLocation(format, date1Str, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ // 将字符串转化为Time格式
|
|
|
+ date2, err := time.ParseInLocation(format, date2Str, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ //计算相差天数
|
|
|
+ return int(date1.Sub(date2).Hours() / 24), nil
|
|
|
+}
|