以分钟为单位截断持续时间

时间:2019-04-12 08:57:39

标签: go time

我想通过使用Go中orderDeliveryStartTime库的Truncate函数从time减去30分钟。而是减去30秒。如何从time.Time中减去30分钟?

package main

import (
    "fmt"
    "time"
)

func main() {
    var pickingTimeConfig int
    pickingTimeConfig = 30

    layoutTime := "2006-01-02 15:04:05"

    pickingTime := time.Duration(pickingTimeConfig) * time.Minute
    fmt.Println(pickingTime.Nanoseconds())
    vcmTimeLocation := time.FixedZone("UTC+7", 25200)

    orderDeliveryStartTime := time.Date(2019, 4, 11, 13, 0, 30, 0, vcmTimeLocation)

    fmt.Println(orderDeliveryStartTime.Format(layoutTime))
    fmt.Println(orderDeliveryStartTime.Truncate(pickingTime).Format(layoutTime))

}

实际结果:

  

1800000000000

     

2019-04-11 13:00:30

     

2019-04-11 13:00:00

预期结果:

  

1800000000000

     

2019-04-11 13:00:30

     

2019-04-11 12:30:30

1 个答案:

答案 0 :(得分:4)

只需使用Time.Add()方法,并传递-30 * time.Minute

t2 := orderDeliveryStartTime.Add(-30 * time.Minute)
fmt.Println(t2.Format(layoutTime))

输出(在Go Playground上尝试):

2019-04-11 13:00:30
2019-04-11 12:30:30