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