dromara / carbon

A simple, semantic and developer-friendly golang package for time.
https://pkg.go.dev/github.com/dromara/carbon/v2
MIT License
4.83k stars 242 forks source link

How to get time.Duration in two carbon instances? ? #223

Closed shuqingzai closed 9 months ago

shuqingzai commented 9 months ago

pkg version: github.com/golang-module/carbon/v2 v2.2.14

目前似乎无法直接比较获取 time.Duration ,只能解析标准库获取??

示例代码

func main() {
        now := carbon.Now()
    tomorrow := carbon.Tomorrow()
    duration := tomorrow.ToStdTime().Sub(now.ToStdTime())
    fmt.Println(duration)
}
gouguoyin commented 9 months ago

Upgrade to v2.3.9, using DiffInDuration or DiffAbsInDuration method, such as

now := carbon.Now()
now.DiffInDuration(now).String() // 0s
now.AddHour().DiffInDuration(now).String() // 1h0m0s
now.SubHour().DiffInDuration(now).String() // -1h0m0s