前端之家收集整理的这篇文章主要介绍了
golang 月工作日,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
需要引入包 "github.com/jinzhu/now"
func WorkDayOfMonth(currentTime string) ([]interface{},int) {
currentDate,_ := time.Parse("2006-01",currentTime)
year := currentDate.Year()
month,_ := strconv.Atoi(currentDate.Month().String())
count := DaysOfMonth(year,month)
bmonth := now.New(currentDate).BeginningOfMonth()
var monthArr []string
var tempArr []string
var weekArr []interface{}
for i := 0; i < count; i++ {
dd := bmonth.AddDate(0, 0,i)
if dd.Weekday().String() != "Saturday" && dd.Weekday().String() != "Sunday" {
monthArr = append(monthArr,dd.Format("2006-01-02"))
if len(tempArr) > 0 {
aa,_ := time.Parse("2006-01-02",tempArr[len(tempArr)-1])
dd,_ = time.Parse("2006-01-02",dd.Format("2006-01-02"))
if dd.Sub(aa).Hours()/24 > 1 {
weekArr = append(weekArr,tempArr)
tempArr = nil
}
}
tempArr = append(tempArr,dd.Format("2006-01-02"))
}
}
return weekArr,count
}
func DaysOfMonth(year int,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 days
}
原文链接:https://www.f2er.com/go/188943.html