Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginning of Month error #45

Closed
lvndry opened this issue Mar 31, 2021 · 1 comment
Closed

Beginning of Month error #45

lvndry opened this issue Mar 31, 2021 · 1 comment
Assignees

Comments

@lvndry
Copy link

lvndry commented Mar 31, 2021

Reproducible Example

https://play.golang.org/p/o21N9NbVLdk

Description

If you run this on the 31st of the month it won't print the beginning of the next month but the beginning of the month after
So on 03-21-2021 it'll print 05-01-201 instead of 04-01-2021

@tc-hib
Copy link

tc-hib commented Apr 7, 2021

Hello,

In your example, AddDate(0, 1, 0) returns 05-01-2021.
This is the expected behavior. From Time.AddDate:

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

You can fix it by adding a month after finding the first day of the month.
playground

@jinzhu jinzhu closed this as completed Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants