time.Time Round to Day

前端 未结 4 1021
野趣味
野趣味 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 14:12

    in addition to sticky's answer to get the local Truncate do like this

    t := time.Date(2015, 4, 2, 0, 15, 30, 918273645, time.Local)

    d := 24 * time.Hour
fmt.Println("in UTC", t.Truncate(d))

    _, dif := t.Zone()

    fmt.Println("in Local", t.Truncate(24 * time.Hour).Add(time.Second * time.Duration(-dif)))
    

提交回复
热议问题