Skip to content

AddDate return error result #20155

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

Closed
LouisYuBin opened this issue Apr 28, 2017 · 1 comment
Closed

AddDate return error result #20155

LouisYuBin opened this issue Apr 28, 2017 · 1 comment

Comments

@LouisYuBin
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go 1.8

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

ubuntu kylin 16.04 LTS

What did you do?

`package main

import (
"time"
"fmt"
)

func main() {

userTime, err := time.Parse("2006-01-02", "2017-12-31")
if err != nil {
	fmt.Println(err)
}
lastMonthTime := userTime.AddDate(0, -1, 0)

fmt.Println(lastMonthTime)
lastMonthMonth := int(lastMonthTime.Month())
fmt.Println(lastMonthMonth)

}
`

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

2017-11-30 00:00:00 +0000 UTC
11

What did you see instead?

2017-12-01 00:00:00 +0000 UTC
12

@ianlancetaylor
Copy link
Contributor

This is behaving as documented. Subtracting one month from "December 31" produces "November 31". Since there is no such date, it is normalized to "December 1". See https://golang.org/pkg/time/#Time.AddDate .

@golang golang locked and limited conversation to collaborators Apr 28, 2018
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

3 participants