golang獲取日期,也可以用於獲取月份的天數
package main import ( "fmt" "strconv" "time" ) func main() { y := "2020" m := "6" result := GetMonthStartAndEnd(y,m) fmt.Print(result) } //GetMonthStartAndEnd 獲取月份的第一天和最后一天 func GetMonthStartAndEnd(myYear string,myMonth string) (map[string]string) { // 數字月份必須前置補零 if len(myMonth)==1 { myMonth = "0"+myMonth } yInt,_ := strconv.Atoi(myYear) timeLayout := "2006-01-02 15:04:05" loc, _ := time.LoadLocation("Local") theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc) newMonth := theTime.Month() t1 := time.Date(yInt,newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02") t2 := time.Date(yInt,newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02") result := map[string]string{"start":t1,"end":t2,} return result }