Subtracting time.Duration from time in Go

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

Subtracting time.Duration from time in Go

问题

我有一个从time.Now()获取的time.Time值,我想得到一个正好是一个月前的时间。

我知道可以使用time.Sub()进行减法运算(需要另一个time.Time值),但这将得到一个time.Duration类型的结果,而我需要得到一个时间值。

英文:

I have a time.Time value obtained from time.Now() and I want to get another time which is exactly 1 month ago.

I know subtracting is possible with time.Sub() (which wants another time.Time), but that will result in a time.Duration and I need it the other way around.

答案1

得分: 209

根据Thomas Browne的评论,因为lnmx的答案只适用于从日期中减去日期,所以这里是对他的代码进行修改,可以从time.Time类型中减去时间。

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println("now:", now)

	count := 10
	then := now.Add(time.Duration(-count) * time.Minute)
	// 如果我们要减去固定数量的单位,可以使用下面的一行代码代替上面的两行代码。它会自动进行类型转换。
	// then := now.Add(-10 * time.Minute)
	fmt.Println("10 minutes ago:", then)
}

输出结果为:

now: 2009-11-10 23:00:00 +0000 UTC
10 minutes ago: 2009-11-10 22:50:00 +0000 UTC

此外,根据需要,您还可以使用time.Hourtime.Second代替time.Minute

英文:

In response to Thomas Browne's comment, because lnmx's answer only works for subtracting a date, here is a modification of his code that works for subtracting time from a time.Time type.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()

    fmt.Println("now:", now)

    count := 10
    then := now.Add(time.Duration(-count) * time.Minute)
    // if we had fix number of units to subtract, we can use following line instead fo above 2 lines. It does type convertion automatically.
    // then := now.Add(-10 * time.Minute)
    fmt.Println("10 minutes ago:", then)
}

Produces:

now: 2009-11-10 23:00:00 +0000 UTC
10 minutes ago: 2009-11-10 22:50:00 +0000 UTC

Not to mention, you can also use time.Hour or time.Second instead of time.Minute as per your needs.

Playground: https://play.golang.org/p/DzzH4SA3izp

答案2

得分: 171

尝试使用AddDate函数:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println("now:", now)

	then := now.AddDate(0, -1, 0)

	fmt.Println("then:", then)
}

输出结果为:

now: 2009-11-10 23:00:00 +0000 UTC
then: 2009-10-10 23:00:00 +0000 UTC

Playground链接:http://play.golang.org/p/QChq02kisT

英文:

Try AddDate:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println("now:", now)

	then := now.AddDate(0, -1, 0)

	fmt.Println("then:", then)
}

Produces:

now: 2009-11-10 23:00:00 +0000 UTC
then: 2009-10-10 23:00:00 +0000 UTC

Playground: http://play.golang.org/p/QChq02kisT

答案3

得分: 75

你可以对 time.Duration 进行否定操作:

then := now.Add(-dur)

你甚至可以将 time.Duration0 进行比较:

if dur > 0 {
    dur = -dur
}

then := now.Add(dur)

你可以在 http://play.golang.org/p/ml7svlL4eW 上看到一个可运行的示例。

英文:

You can negate a time.Duration:

then := now.Add(- dur)

You can even compare a time.Duration against 0:

if dur > 0 {
	dur = - dur
}

then := now.Add(dur)

You can see a working example at http://play.golang.org/p/ml7svlL4eW

答案4

得分: 4

有一个time.ParseDuration函数可以接受负数的持续时间,具体请参考官方文档。换句话说,如果你可以直接得到一个准确的持续时间,就没有必要对持续时间取反。

例如,当你需要减去一个小时半的时间时,可以这样做:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println("now:", now)

	duration, _ := time.ParseDuration("-1.5h")

	then := now.Add(duration)

	fmt.Println("then:", then)
}

https://play.golang.org/p/63p-T9uFcZo

英文:

There's time.ParseDuration which will happily accept negative durations, as per manual. Otherwise put, there's no need to negate a duration where you can get an exact duration in the first place.

E.g. when you need to substract an hour and a half, you can do that like so:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println("now:", now)

	duration, _ := time.ParseDuration("-1.5h")

	then := now.Add(duration)

	fmt.Println("then:", then)
}

https://play.golang.org/p/63p-T9uFcZo

huangapple
  • 本文由 发表于 2014年10月10日 02:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/26285735.html
匿名

发表评论

匿名网友

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

确定