比较两个日期而不考虑时间

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

Comparing two dates without taking time into account

问题

我有两个日期,我想比较的只是日期,忽略时间。目前我有以下代码:

package main

import (
	"time"
	//"fmt"
)

func main() {
    a, _ := time.Parse(time.RFC3339, "2017-02-01T12:00:00+00:00")
	b, _ := time.Parse(time.RFC3339, "2017-02-11T14:30:00+00:00")

	x := b.Sub(a)
	
	println(int(x.Hours()))
}

这段代码输出 242。这是正确的,但实际上我想要的是这样比较日期:

a, _ := time.Parse(time.RFC3339, "2017-02-01T00:00:00+00:00")
b, _ := time.Parse(time.RFC3339, "2017-02-11T00:00:00+00:00")

注意:分钟/小时/秒被设置为零 - 现在的差值将是 240 小时。

我无法找到如何实现这一点,Go 中是否有一个 time.SetTime(0, 0, 0) 函数,我是否错过了它,或者有没有其他的方法来重置日期的时间?

英文:

I have two dates like this, I would like to compare only the dates, ignoring the time. Currently I have this:

package main

import (
	"time"
	//"fmt"
)

func main() {
    a, _ := time.Parse(time.RFC3339, "2017-02-01T12:00:00+00:00")
	b, _ := time.Parse(time.RFC3339, "2017-02-11T14:30:00+00:00")

	x := b.Sub(a)
	
	println(int(x.Hours()))
}

Which prints 242. That is correct, but what I actually want to do is compare the dates like this:

    a, _ := time.Parse(time.RFC3339, "2017-02-01T00:00:00+00:00")
	b, _ := time.Parse(time.RFC3339, "2017-02-11T00:00:00+00:00")

Notice: minutes/hours/seconds have been set to zero - the diff will now be 240 hours.

I couldn't really figure out how to do this, is there a time.SetTime(0, 0, 0) function in Go that I missed or what's the canonical way to reset the time for a date?

答案1

得分: 4

你可以使用Truncate方法将时间戳截断为最接近的整天倍数。

在你的例子中:

oneDay := 24 * time.Hour
a = a.Truncate(oneDay)
b = b.Truncate(oneDay)

你可以在这里找到已经适配的代码的playground:https://play.golang.org/p/yWIYt3UkiT

英文:

You could Truncate the times to make them round to a multiple of a day.

In your example:

oneDay := 24 * time.Hour
a = a.Truncate(oneDay)
b = b.Truncate(oneDay)

Find a playground with the adapted code here: https://play.golang.org/p/yWIYt3UkiT

答案2

得分: 0

如果使用time.Time处理日期,你必须非常小心,因为在夏令时的开始和结束时可能会出现边界情况。

无耻地打个广告:我写了一个日期包(实际上是从别人的早期工作中衍生出来的),用于处理日期而不会遇到夏令时的问题。

https://github.com/rickb777/date

你可以这样做:

a := date.New(2017, 2, 1)
b := date.New(2017, 2, 11)
daysDifference := b.Sub(a)
fmt.Println(daysDifference)

还有其他的子包可以处理日期范围、时间跨度、ISO8601周期和时钟时间。

英文:

You have to be rather careful handling dates if using time.Time because there can be edge cases around the beginning and ending of daylight savings.

Shameless plug: I wrote a Date package (actually it was derived from someone else's earlier work) to deal with dates without incurring the DST issues.

https://github.com/rickb777/date

You can do, for example,

a := date.New(2017, 2, 1)
b := date.New(2017, 2, 11)
daysDifference := b.Sub(a)
fmt.Println(daysDifference)

There are other sub-packages to handle date ranges, timespans, ISO8601 periods and clock-face time.

huangapple
  • 本文由 发表于 2017年7月11日 10:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/45024526.html
匿名

发表评论

匿名网友

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

确定