Skip to content

time AddDate() Calculate time last month #71069

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
djxhhh opened this issue Dec 31, 2024 · 3 comments
Closed

time AddDate() Calculate time last month #71069

djxhhh opened this issue Dec 31, 2024 · 3 comments

Comments

@djxhhh
Copy link

djxhhh commented Dec 31, 2024

Go version

go1.22.4 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Mayn\AppData\Local\go-build
set GOENV=C:\Users\Mayn\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Mayn\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Mayn\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:/Program Files/Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=D:\gamecomponents\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Mayn\AppData\Local\Temp\go-build4203366999=/tmp/go-build -gno-record-gcc-switches

What did you do?

func main() {
date := time.Date(2023, 12, 31, 0, 0, 0, 0, time.UTC)
lastMonth := date.AddDate(0, -1, 0)
fmt.Println("last year last month time:", lastMonth.Format("2006-01-02"))
now := time.Now().UTC()
nowLastMonth := now.AddDate(0, -1, 0)
fmt.Println("now last month time:", nowLastMonth.Format("2006-01-02"))
}

What did you see happen?

out: last year last month time: 2023-12-01
now last month time: 2024-12-01

What did you expect to see?

out: last year last month time: 2023-11-30
now last month time: 2024-11-30

I can achieve the effect myself through other methods, but I hope the standard package can fix this problem. I want to know if this problem has been fixed in other versions. If it has been fixed, please let me know the corresponding version.

@djxhhh
Copy link
Author

djxhhh commented Dec 31, 2024

I think that for the operation of getting last month, I should give me the data of last month, right? Now that the method month is valid, the last month and this month must have the same current date? Then I think the month parameter is a bit confusing.

@seankhliao
Copy link
Member

you get Nov 31, which is normalized to Dec 1.
this is also described in the docs for the function.
closing as working as intended.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2024
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