|
@@ -1118,3 +1118,55 @@ func GetTimeSubDay(t1, t2 time.Time) int {
|
|
|
|
|
|
return day
|
|
|
}
|
|
|
+
|
|
|
+// AppendMonthDayFromDiffDate 日期补充完整
|
|
|
+func AppendMonthDayFromDiffDate(strDate string) (newDate string, err error) {
|
|
|
+ // 如果能直接解析成完整年月日的格式, 那么直接返回
|
|
|
+ tryDate := []string{"2006-01-02", "2006/01/02", "2006-1-2", "2006/1/2"}
|
|
|
+ for _, v := range tryDate {
|
|
|
+ t, e := time.ParseInLocation(v, strDate, time.Local)
|
|
|
+ if e == nil {
|
|
|
+ newDate = t.Format("2006-01-02")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试解析月份格式-补充日
|
|
|
+ tryDate = []string{"2006-01", "2006/01", "2006-1", "2006/1", "2006年1月"}
|
|
|
+ var date time.Time
|
|
|
+ for _, v := range tryDate {
|
|
|
+ d, e := time.ParseInLocation(v, strDate, time.Local)
|
|
|
+ if e == nil {
|
|
|
+ date = d
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !date.IsZero() {
|
|
|
+ strMonth := fmt.Sprintf("%d", date.Month())
|
|
|
+ if date.Month() < 10 {
|
|
|
+ strMonth = fmt.Sprintf("0%s", strMonth)
|
|
|
+ }
|
|
|
+ m := fmt.Sprintf("%d-%s-01 00:00:00", date.Year(), strMonth)
|
|
|
+ preTime, e := time.ParseInLocation("2006-01-02 15:04:05", m, time.Local)
|
|
|
+ if e != nil {
|
|
|
+ return "", e
|
|
|
+ }
|
|
|
+ newDate = time.Date(date.Year(), preTime.Month()+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试解析年份格式-补充月日
|
|
|
+ tryDate = []string{"2006", "2006年"}
|
|
|
+ for _, v := range tryDate {
|
|
|
+ d, e := time.ParseInLocation(v, strDate, time.Local)
|
|
|
+ if e == nil {
|
|
|
+ date = d
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if date.IsZero() {
|
|
|
+ return "", fmt.Errorf("时间格式错误, str: %s", strDate)
|
|
|
+ }
|
|
|
+ newDate = time.Date(date.Year(), 12, 31, 0, 0, 0, 0, time.Local).Format("2006-01-02")
|
|
|
+ return
|
|
|
+}
|