英文:
Golang Time.Sub returns hours, mins, seconds - how to get it in days or months?
问题
在Golang中,我正在尝试使用time.Sub来找出给定的time.Time值与当前time.Time值之间的差异,我的结果看起来像这样 -
4691h42m17.1831189s
是否有可能使其返回天数或月数,而不进行任何数学操作,只使用time包?
英文:
In Golang, I am trying to find out the difference between a given time.Time value and the current time.Time value using time.Sub and my result looks like this -
4691h42m17.1831189s
Is it possible to make it return the number of days or months without performing any mathematical manipulations on this value and just using the time package?
答案1
得分: 2
不,这是不可能的。time.Sub 返回的 Duration 类型仅限于小时:
> 为了避免在夏令时转换时区时产生混淆,没有定义天或更长时间单位。
英文:
No it is not possible. The type Duration that is returned by time.Sub is limited to hours:
> There is no definition for units of Day or larger to avoid confusion across daylight savings time zone transitions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论