dounue6984 2015-12-07 08:30
浏览 457
已采纳

获取3年前的Golang时间戳记?

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条回答 默认 最新

  • dongzhi9192 2015-12-07 08:47
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题