time.Time Round to Day

前端 未结 4 1032
野趣味
野趣味 2021-02-01 13:32

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,

4条回答
  •  庸人自扰
    2021-02-01 13:54

    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

提交回复
热议问题