golang time format code example
Example 1: go packae unix time
package main
import (
"fmt"
"time"
)
func unixtime() {
fmt.Println(time.Now().Unix())
}
Example 2: golang time format with milliseconds
package main
import (
"fmt"
"time"
)
func main() {
timeFormatMilisecondsWithTrailingZero()
timeFormatMilisecondsWithoutTrailingZero()
}
func timeFormatMilisecondsWithTrailingZero() {
fmt.Println(time.Now().Format("2006-01-02 15:04:05.000"))
}
func timeFormatMilisecondsWithoutTrailingZero() {
fmt.Println(time.Now().Format("2006-01-02 15:04:05.999"))
}
Example 3: date format golang
input := "2017-08-31"
layout := "2006-01-02"
t, _ := time.Parse(layout, input)
fmt.Println(t)
fmt.Println(t.Format("02-Jan-2006"))