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

20201231 user time.AddDate(0,-1,0), result is 20201201 #43457

Closed
zhaobingchun opened this issue Jan 1, 2021 · 2 comments
Closed

20201231 user time.AddDate(0,-1,0), result is 20201201 #43457

zhaobingchun opened this issue Jan 1, 2021 · 2 comments

Comments

@zhaobingchun
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.15.4 windows/amd64

Does this issue reproduce with the latest release?

source code

func main() {
	date := time.Date(2020, 12, 31, 0, 0, 0, 0, time.Local)
	t := date.AddDate(0, -1, 0)
	fmt.Println("t:", t)
	strMonth := t.Format("200601")
	fmt.Println("month:", strMonth)
}

result

t: 2020-12-01 00:00:00 +0800 CST
month: 202012

area: china

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

What did you expect to see?

What did you see instead?

@AlexRouSg
Copy link
Contributor

https://golang.org/pkg/time/#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.

20201231 - 1 month = 20201131
20201131 does not exist -> 20201201

@ALTree
Copy link
Member

ALTree commented Jan 1, 2021

What @AlexRouSg said. Closing here since this is working as intended.

@ALTree ALTree closed this as completed Jan 1, 2021
@golang golang locked and limited conversation to collaborators Jan 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants