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
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM