Epoch
// Importing necessary packages.
import (
"fmt"
"time"
)
// The main function, where the execution of the program begins.
func main() {
// Getting the current time.
now := time.Now()
fmt.Println(now)
// Converting the current time to Unix timestamp at different precisions.
// Unix timestamp in seconds.
fmt.Println(now.Unix())
// Unix timestamp in milliseconds.
fmt.Println(now.UnixMilli())
// Unix timestamp in nanoseconds.
fmt.Println(now.UnixNano())
// Converting Unix timestamp back to time.Time.
// Creating a time.Time from Unix timestamp in seconds.
fmt.Println(time.Unix(now.Unix(), 0))
// Creating a time.Time from Unix timestamp in nanoseconds.
fmt.Println(time.Unix(0, now.UnixNano()))
}Output
Last updated