gpt4 book ai didi

go - 按分钟截断持续时间

转载 作者:IT王子 更新时间:2023-10-29 02:28:19 26 4
gpt4 key购买 nike

我想通过使用 Go 中 time 库的 Truncate 函数从 orderDeliveryStartTime 中减去 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

最佳答案

只需使用 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

关于go - 按分钟截断持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com