Golang的Time.Sub函数返回小时、分钟和秒数,如何将其转换为天数或月数?

huangapple go评论102阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年9月9日 20:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/39411510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定