减去时间.Go中时间的持续时间 [英] Subtracting time.Duration from time in Go
本文介绍了减去时间.Go中时间的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从 time.Now()
获得的 time.Time
值,我想获取另一个正好是1个月前的时间.
I have a time.Time
value obtained from time.Now()
and I want to get another time which is exactly 1 month ago.
我知道可以通过 time.Sub()
(需要另一个 time.Time
)进行减法,但这将导致 time.Duration
,而我反过来需要它.
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.
推荐答案
尝试添加日期:
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
游乐场: http://play.golang.org/p/QChq02kisT
这篇关于减去时间.Go中时间的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文