Goのtimeで翌月を取得する

2021/09/22に公開

うまくいかなかったやりかた

以下で実行すると、月末などで2ヶ月プラスされるケースがあった。
31日で翌月が30日までの場合でも31日足されている??

nextMonth := time.Now().AddDate(0, 1, 0)

こっちだとうまくいきそう

now := time.Now()
currentYear, currentMonth, _ := now.Date()
currentLocation := now.Location()

nextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)

Discussion