1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
func GetMonInfo(t1, t2 time.Time) ([]MonInfo, bool) { sameMonth := false result := make([]MonInfo, 0) if t1.Format("200601") == t2.Format("200601") { result = append(result, MonInfo{ StartDay: t1.Format("2006-01-02 15:04:05"), EndDay: t2.Format("2006-01-02 15:04:05"), MonStr: t2.Format("200601"), }) sameMonth = true } else {
endT1 := time.Date(t1.Year(), t1.Month()+1, 1, 0, 0, 0, -1, t1.Location())
result = append(result, MonInfo{ StartDay: t1.Format("2006-01-02 15:04:05"), EndDay: endT1.Format("2006-01-02 15:04:05"), MonStr: t1.Format("200601"), })
t1Tmp := time.Date(t1.Year(), t1.Month(), 1, 0, 0, 0, 0, t1.Location())
var i = 1 for { t3 := t1Tmp.AddDate(0, i, 0) if t3.Format("200601") == t2.Format("200601") { startT2 := time.Date(t2.Year(), t2.Month(), 1, 0, 0, 0, 0, t2.Location())
result = append(result, MonInfo{ StartDay: startT2.Format("2006-01-02 15:04:05"), EndDay: t2.Format("2006-01-02 15:04:05"), MonStr: t2.Format("200601"), }) break } else { startT3 := time.Date(t3.Year(), t3.Month(), 1, 0, 0, 0, 0, t3.Location()) endT3 := time.Date(t3.Year(), t3.Month()+1, 1, 0, 0, 0, -1, t3.Location()) result = append(result, MonInfo{ StartDay: startT3.Format("2006-01-02 15:04:05"), EndDay: endT3.Format("2006-01-02 15:04:05"), MonStr: t3.Format("200601"), }) } i++
if i > 12 { break } } }
return result, sameMonth }
type MonInfo struct { StartDay string EndDay string MonStr string }
|