github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgTime/MonthDate.go (about) 1 package kmgTime 2 3 import "time" 4 5 //精确到月份的日期,不携带时区信息 格式如:201407 6 type MonthDate uint32 7 8 func (m MonthDate) IsValid() bool { 9 if m.Month() <= 0 || m.Month() > 12 { 10 return false 11 } 12 return true 13 } 14 func (m MonthDate) Year() int { 15 return int(m / 100) 16 } 17 func (m MonthDate) Month() time.Month { 18 return time.Month(m % 100) 19 } 20 func MonthDateFromTime(t time.Time) MonthDate { 21 return MonthDate(uint32(t.Year())*100 + uint32(t.Month())) 22 } 23 24 //这个月的天数 25 func (m MonthDate) DayNum() int { 26 month := m.Month() 27 year := m.Year() 28 if month == time.February { 29 if year%4 == 0 && (year%100 != 0 || year%400 == 0) { // leap year 30 return 29 31 } 32 return 28 33 } 34 35 if month <= 7 { 36 month++ 37 } 38 if month&0x0001 == 0 { 39 return 31 40 } 41 return 30 42 }