douchi8503 2016-05-02 18:00
浏览 12
已采纳

时间到一天的时间

I have a timestamp coming in, I wonder if there's a way to round it down to the start of a day in PST. For example, ts: 1305861602 corresponds to 2016-04-14, 21:10:27 -0700, but I want to round it to a timestamp that maps to 2016-04-14 00:00:00 -0700. I read through the time.Time doc but didn't find a way to do it.

  • 写回答

2条回答 默认 最新

  • douqufan9148 2016-05-02 18:11
    关注

    The simple way to do this is to create new Time using the previous one and only assigning the year month and day. It would look like this;

    rounded := time.Date(toRound.Year(), toRound.Month(), toRound.Day(), 0, 0, 0, 0, toRound.Location())
    

    here's a play example; https://play.golang.org/p/jnFuZxruKm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?