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 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况