We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
time.AddDate
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
go version go1.21.6 darwin/arm64
go env
go env GO111MODULE='on' GOARCH='arm64' GOBIN='/usr/local/go/bin' GOCACHE='/Users/yanmeng/Library/Caches/go-build' GOENV='/Users/yanmeng/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/yanmeng/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/yanmeng/go' GOPRIVATE='' GOPROXY='https://goproxy.cn' GOROOT='/usr/local/go' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.21.6' GCCGO='gccgo' AR='ar' CC='clang' CXX='clang++' CGO_ENABLED='1' GOMOD='/Users/yanmeng/Workspace/redhat/hub-of-hubs/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/4t/fhhxz9q56vd0mk9t7ytjpvbm0000gn/T/go-build2116447077=/tmp/go-build -gno-record-gcc-switches -fno-common'
I want to obtain the current time for the previous month and the month before the previous month. I ran the following code:
package main import ( "fmt" "time" ) func main() { current := time.Now() date1 := current.AddDate(0, -1, 0) date2 := current.AddDate(0, -2, 0) fmt.Println("date", current) fmt.Println("date1", date1) fmt.Println("date2", date2) }
It echoes the following result
$ go run time/main.go [16:05:27] date 2024-01-31 16:05:38.159025 +0800 CST m=+0.000179293 date1 2023-12-31 16:05:38.159025 +0800 CST date2 2023-12-01 16:05:38.159025 +0800 CST
$ go run time/main.go [16:05:27] date 2024-01-31 16:05:38.159025 +0800 CST m=+0.000179293 date1 2023-12-31 16:05:38.159025 +0800 CST date2 2023-11-31 16:05:38.159025 +0800 CST
The text was updated successfully, but these errors were encountered:
Close it cause there is no 31th for some months
Sorry, something went wrong.
Ref:#31145
No branches or pull requests
Go version
go version go1.21.6 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
I want to obtain the current time for the previous month and the month before the previous month. I ran the following code:
What did you see happen?
It echoes the following result
What did you expect to see?
The text was updated successfully, but these errors were encountered: