Go 有方法可以提取时间戳的几乎所有组件,例如time.Second(), time.Nano(),但没有提取时间戳的毫秒部分的方法。
如何提取时间戳的毫秒值。
例如,在时间戳的情况下,如:
2021-01-07 10:33:06.511
Run Code Online (Sandbox Code Playgroud)
我想提取 511
要访问小数秒,您可以使用time.Nanosecond(). 如果我们将它转换为time.Duration(time.Duration正是纳秒计数),我们可以利用它的Duration.Milliseconds()方法 (这当然没有魔法但代码会更清晰易读):
func extractMs(t time.Time) int64 {
return time.Duration(t.Nanosecond()).Milliseconds()
}
Run Code Online (Sandbox Code Playgroud)
在Go Playground上试一试。