Get 3 years ago timestamp in golang?

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

Get 3 years ago timestamp in golang?

问题

我想要获取3年前的Unix时间戳,我现在使用的代码是:

time.Now().Unix() - 3 * 3600 * 24 * 365

不过这种方法并不是非常准确(因为一年可能有366天)。

英文:

I want to get the time unix time stamp 3 years ago, I use this now:

time.Now().Unix(() - 3 * 3600 * 24 * 365

This is not that accurate anyway (because year may have 366 days).

答案1

得分: 46

只需使用Time.AddDate()方法,您可以指定要添加的年份、月份和天数,如果要回到过去,这些值也可以是负数:

AddDate()方法声明:

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

示例:

t := time.Now()
fmt.Println(t)
t2 := t.AddDate(-3, 0, 0)
fmt.Println(t2)

输出结果(在Go Playground上尝试):

2009-11-10 23:00:00 +0000 UTC
2006-11-10 23:00:00 +0000 UTC
英文:

Simply use the Time.AddDate() method where you can specify the time to add in years, months and days, all of which can also be negative if you want to go back in time:

AddDate() method declaration:

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

Example:

t := time.Now()
fmt.Println(t)
t2 := t.AddDate(-3, 0, 0)
fmt.Println(t2)

Output (try it on the Go Playground):

2009-11-10 23:00:00 +0000 UTC
2006-11-10 23:00:00 +0000 UTC

答案2

得分: 2

也许你可以这样做:

package main

import (
	"fmt"
	"time"
)

func timeShift(now time.Time, timeType string, shift int) time.Time {
	var shiftTime time.Time
	switch timeType {
	case "year":
		shiftTime = now.AddDate(shift, 0, 0)
	case "month":
		shiftTime = now.AddDate(0, shift, 0)
	case "day":
		shiftTime = now.AddDate(0, 0, shift)
	case "hour":
		shiftTime = now.Add(time.Hour * time.Duration(shift))
	case "minute":
		shiftTime = now.Add(time.Minute * time.Duration(shift))
	case "second":
		shiftTime = now.Add(time.Second * time.Duration(shift))
	default:
		shiftTime = now
	}
	return shiftTime
}

func main() {
	d := time.Now().UTC()
	fmt.Println(timeShift(d, "month", 1))
	fmt.Println(timeShift(d, "day", -1))
	fmt.Println(timeShift(d, "hour", -1))
	fmt.Println(timeShift(d, "minute", -1))
	fmt.Println(timeShift(d, "second", -1))
}

这段代码定义了一个 timeShift 函数,它接受当前时间 now、时间类型 timeType 和偏移量 shift 作为参数,并返回偏移后的时间。在 main 函数中,我们获取当前时间 d,然后分别调用 timeShift 函数来进行时间偏移,并打印结果。

英文:

Maybe you can do this

package main

import (
	"fmt"
	"time"
)

func timeShift(now time.Time, timeType string, shift int) time.Time {
	var shiftTime time.Time
	switch timeType {
	case "year":
		shiftTime = now.AddDate(shift, 0, 0)
	case "month":
		shiftTime = now.AddDate(0, shift, 0)
	case "day":
		shiftTime = now.AddDate(0, 0, shift)
	case "hour":
		shiftTime = now.Add(time.Hour * time.Duration(shift))
	case "minute":
		shiftTime = now.Add(time.Minute * time.Duration(shift))
	case "second":
		shiftTime = now.Add(time.Second * time.Duration(shift))
	default:
		shiftTime = now
	}
	return shiftTime
}

func main(){
	d := time.Now().UTC()
	fmt.Println(timeShift(d, "month", 1))
	fmt.Println(timeShift(d, "day", -1))
	fmt.Println(timeShift(d, "hour", -1))
	fmt.Println(timeShift(d, "minute", -1))
	fmt.Println(timeShift(d, "second", -1))
}


</details>



huangapple
  • 本文由 发表于 2015年12月7日 16:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/34129276.html
匿名

发表评论

匿名网友

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

确定