Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shuqingzai opened this issue Feb 19, 2024 · 1 comment
Closed

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

shuqingzai opened this issue Feb 19, 2024 · 1 comment
Assignees
Labels
Question Indicates that an issue, pull request, or discussion needs more information

Comments

@shuqingzai
Copy link

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)
}
@shuqingzai shuqingzai added the Question Indicates that an issue, pull request, or discussion needs more information label Feb 19, 2024
@Issues-translate-bot Issues-translate-bot changed the title 在两个 carbon 实例中怎么获取 time.Duration ?? How to get time.Duration in two carbon instances? ? Feb 19, 2024
@gouguoyin
Copy link
Member

gouguoyin commented Feb 20, 2024

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

@gouguoyin gouguoyin self-assigned this Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Indicates that an issue, pull request, or discussion needs more information
Development

No branches or pull requests

2 participants