我需要查询我的数据库以了解一小时内发生的事件。因此,我想获取从现在到那时(现在 - 24 小时,或现在 - 1 天)之间的事件。
我尝试过这种方法,但它是不正确的 -
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// print the time now
fmt.Println(now)
then := time.Now()
diff := 24
diff = diff.Hours()
then = then.Add(-diff)
// print the time before 24 hours
fmt.Println(then)
// print the delta between 'now' and 'then'
fmt.Println(now.Sub(then))
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使then == 1 全天/24 小时之前?
非常感谢你的帮助!!
使用时间包中提供的持续时间常量,例如time.Hour
diff := 24 * time.Hour
then := time.Now().Add(-diff)
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要前一天的同一时间(可能不是早 24 小时,http://play.golang.org/p/B32RbtUuuS)
then := time.Now().AddDate(0, 0, -1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2970 次 |
| 最近记录: |