golang time.millisecond * int code example

Example 1: golang time format with milliseconds

//Golang format datetime to string with miliseconds
package main

import (
	"fmt"
	"time"
)

func main() {
	timeFormatMilisecondsWithTrailingZero()
	timeFormatMilisecondsWithoutTrailingZero()
}

func timeFormatMilisecondsWithTrailingZero() {
	//2020-10-22 14:30:29.250
	fmt.Println(time.Now().Format("2006-01-02 15:04:05.000"))

}
func timeFormatMilisecondsWithoutTrailingZero() {
	//2020-10-22 14:30:29.25
	fmt.Println(time.Now().Format("2006-01-02 15:04:05.999"))
}

Example 2: time now golang int64

now := time.Now()      // current local time
sec := now.Unix()      // number of seconds since January 1, 1970 UTC
nsec := now.UnixNano() // number of nanoseconds since January 1, 1970 UTC

fmt.Println(now)  // time.Time
fmt.Println(sec)  // int64
fmt.Println(nsec) // int64

Tags:

Go Example