golang获取某一年某一月份的开始日期和结束日期

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
}

 

posted @ 2020-02-15 16:45  许伟强  阅读(653)  评论(0编辑  收藏