在Go / Golang中获取当月的第一天和最后一天? [英] Get the first and last day of current month in Go/Golang?
本文介绍了在Go / Golang中获取当月的第一天和最后一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取本月的第一天和最后一天。您可以添加天数和小时数,但不能添加月数,我当时想从下个月减去某天来获得本月的最后一天。像这样的东西:
I'm trying to get the first and last day of the current month. You can add days and hours but not the month, which I was thinking of subtracting one day from the next month to get the last day of this month. Something like this:
package main
import (
"fmt"
"time"
)
func main() {
date := time.Now()
nextMonth := date.Add(time.Month)
LastDay := nextMonth.Add(-time.Hour * 24)
fmt.Println(LastDay)
}
推荐答案
您可以使用 now 库,它非常简单:
You can use now library, it really simple :
now.BeginningOfMonth() // 2013-11-01 00:00:00 Fri
now.EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
请在此处查看详细信息: https://github.com/jinzhu/now
Please take a look here for detail : https://github.com/jinzhu/now
这篇关于在Go / Golang中获取当月的第一天和最后一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文