How do I set a time in the future using the time package in Golang

huangapple go评论73阅读模式
英文:

How do I set a time in the future using the time package in Golang

问题

我希望使用Go语言设置一个未来的时间,目前我的代码只能设置当前时间(time.now())。我想设置一个在今天之后两天的时间(2021年10月18日)。

以下是我的代码:

someTimeInTheFuture = time.now();

我想将"someTimeInTheFuture"设置为今天之后的两天。我该如何做到这一点?

英文:

I wish to set a time in the future using Go, currently my code only sets the time in the present moment (time.now()) I would like to set a time 2 days after today (18/10/2021)

Here is my code

someTimeInTheFuture = time.now();

I would like to set "someTimeInTheFuture" to 2 days after today. How would I go about doing this?

答案1

得分: 2

更加灵活的写法是:time.Now().Add(48 * time.Hour)
详细信息请参考:https://pkg.go.dev/time@go1.17.2#Time.Add

英文:

And more flexible: time.Now().Add(48 * time.Hour)
https://pkg.go.dev/time@go1.17.2#Time.Add

huangapple
  • 本文由 发表于 2021年10月18日 09:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/69609966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定