golang 年初,年末,月初,月末

作者: adm 分类: go 发布时间: 2023-05-23

golang 年初,年末,月初,月末

/**
     * @Author QY
     * @Email 80013593@qq.com
     * @Description //年初
     * @Date 11:33 AM 5/23/23
     * @Param
     * @return
**/
func YearBegin() string {
	t := time.Now()
	first := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, time.UTC).Format("2006-01-02 15:04:05")
	return first
}

/**
     * @Author QY
     * @Email 80013593@qq.com
     * @Description //年初
     * @Date 11:33 AM 5/23/23
     * @Param
     * @return
**/

func YearEnd() string {
	t := time.Now()
	last := time.Date(t.Year(), time.December, 31, 23, 59, 59, 0, time.UTC).Format("2006-01-02 15:04:05")
	return last
}

//GetMonthStartAndEnd 获取月份的第一天和最后一天
func GetMonthStartAndEnd(year, month int) (start, end string) {
	myYear := strconv.Itoa(year)
	myMonth := strconv.Itoa(month)
	// 数字月份必须前置补零
	if len(myMonth) == 1 {
		myMonth = "0" + myMonth
	}

	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(year, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02 15:04:05")
	t2 := time.Date(year, newMonth+1, 0, 23, 59, 59, 0, time.Local).Format("2006-01-02 15:04:05")
	return t1, t2
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!