I need to query my db for events occurred within a single hour. Therefore, I want to get events between now and then (which is now - 24 hours, or now - 1 full day).
I tried this approach, but it is incorrect -
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))
}
How can I make then == 1 full day / 24 hours before now ?
Thanks a lot for your help!!
Use the Duration constants provided in the time package, like time.Hour
diff := 24 * time.Hour
then := time.Now().Add(-diff)
Or if you want the same time on the previous day (which may not be 24 hours earlier, http://play.golang.org/p/B32RbtUuuS)
then := time.Now().AddDate(0, 0, -1)