Time in Seconds Since Start of Day?

Viewed 455

Here's one way to get the number seconds since start of day:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	startOfDay := now.Truncate(24 * time.Hour)
	diffInSeconds := now.Sub(startOfDay).Truncate(1 * time.Second).Seconds()
	fmt.Println(diffInSeconds)
}

Is this approach ok – or is there a better way?

0 Answers