Add days to date in Go

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

Add days to date in Go

问题

我正在尝试在Go语言中给一个已有的日期添加一定数量的天数(实际上是一定数量的周数)。我尝试了以下代码:

myDate.Add(time.Hour * 24 * 7 * weeksToAdd)

但是当我尝试构建时,出现了错误:invalid operation: time.Hour * startAdd (mismatched types time.Duration and float64)

所以,weeksToAdd 目前是一个 float64 类型,但我可以将其更改为 int 或其他类型。将其更改为 int 后,错误信息变为 intDuration 不能相乘。

我该如何给日期添加天数?

英文:

I'm trying to add a number of days (actually a number of weeks) to an existing date in Go. I have tried
myDate.Add(time.Hour * 24 * 7 * weeksToAdd)

But I get an error when I try to build: invalid operation: time.Hour * startAdd (mismatched types time.Duration and float64)

So weeksToAdd is currently a float64, but I can change it to an int or whatever. Changing it to an int only changed my error to say that int and Duration can't be multiplied.

How do I add days to a date?

答案1

得分: 113

使用Time.AddDate()函数:

func (t Time) AddDate(years int, months int, days int) Time

示例:

myDate.AddDate(0, 0, 7 * weeksToAdd)
英文:

Use Time.AddDate():

func (t Time) AddDate(years int, months int, days int) Time

Example:

myDate.AddDate(0, 0, 7 * weeksToAdd)

答案2

得分: 29

你需要将weeksToAdd转换为time.Duration类型:

myDate.Add(time.Hour * 24 * 7 * time.Duration(weeksToAdd))

在Go语言中,尽管time.Duration在技术上是int64类型,但类型别名不能互相替换使用。

此外,在这里,尽管数字常量24和7没有明确指定类型,但仍然可以直接使用,详细解释请参考https://blog.golang.org/constants。

你可以在http://play.golang.org/p/86TFFlixWj中找到一个运行示例。

正如在评论和另一个答案中提到的,当处理超过24小时的持续时间时,使用time.AddDate()比使用time.Add()更可取,因为time.Duration基本上表示纳秒。在处理天、周、月和年时,需要非常小心,因为涉及到夏令时、闰年和潜在的闰秒等问题。

time.Duration类型和表示单位的相关常量的文档强调了这个问题(https://golang.org/pkg/time/#Duration):

为了避免在夏令时区转换时产生混淆,没有定义大于或等于一天的单位。

英文:

You need to convert weeksToAdd into a time.Duration:

myDate.Add(time.Hour * 24 * 7 * time.Duration(weeksToAdd))

In Go, type aliases cannot be used interchangeably even though time.Duration is technically an int64.

Also, here, even though the numeric constants 24 and 7 are not explicitly typed, they can still be used as-is, see https://blog.golang.org/constants for an in-depth explanation.

See http://play.golang.org/p/86TFFlixWj for a running example.

As mentioned in comments and another answer, the use of time.AddDate() is preferable to time.Add() when working on duration superior to 24 hours, since time.Duration basically represents nanoseconds. When working with days, weeks, months and years, a lot of care has to be taken because of things such as daylight saving times, leap years, and maybe potentially leap seconds.

The documentation for time.Duration type and the associated constants representing units emphasize this issue (https://golang.org/pkg/time/#Duration):

> There is no definition for units of Day or larger to avoid confusion across daylight savings time zone transitions.

huangapple
  • 本文由 发表于 2015年10月8日 00:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/32998110.html
匿名

发表评论

匿名网友

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

确定