DEV Community

Mahbub Zulkarnain
Mahbub Zulkarnain

Posted on • Updated on

Golang Time Format

GitHub logo gomodul / waktu

go get github.com/gomodul/waktu

Year

format: 2006, 06

fmt.Println(time.Now().Format("2006")) // 1993
fmt.Println(time.Now().Format("06")) // 93
Enter fullscreen mode Exit fullscreen mode

Month

format: January, Jan, 01, 1

fmt.Println(time.Now().Format("January")) // September
fmt.Println(time.Now().Format("Jan")) // Sep

fmt.Println(time.Now().Format("01")) // 10, 09
fmt.Println(time.Now().Format("1")) // 10, 9 
Enter fullscreen mode Exit fullscreen mode

Date

format: 02, 2

fmt.Println(time.Now().Format("02")) // 10, 09
fmt.Println(time.Now().Format("2")) // 10, 9
Enter fullscreen mode Exit fullscreen mode

Day

format: Monday, Mon

fmt.Println(time.Now().Format("Monday")) // Friday
fmt.Println(time.Now().Format("Mon")) // Fri
Enter fullscreen mode Exit fullscreen mode

Hour

format: 15, 03, 3, PM

// 24 hours
fmt.Println(time.Now().Format("15")) // 13 

// 12 hours
fmt.Println(time.Now().Format("03")) // 01, 12
fmt.Println(time.Now().Format("3")) // 1, 12
fmt.Println(time.Now().Format("PM")) // AM, PM
Enter fullscreen mode Exit fullscreen mode

Minute

format: 04, 4

fmt.Println(time.Now().Format("04")) // 37, 01
fmt.Println(time.Now().Format("4")) // 37, 1
Enter fullscreen mode Exit fullscreen mode

Second

format: 05, 5

fmt.Println(time.Now().Format("05")) // 11, 01
fmt.Println(time.Now().Format("5")) // 11, 1
Enter fullscreen mode Exit fullscreen mode

Nano Second

format: .999999, .999, .99, .9

// Now
fmt.Println(time.Now().Nanosecond()) // 130037000

// Format
fmt.Println(time.Now().Format(".999999")) // 130037
fmt.Println(time.Now().Format(".999")) // 13
fmt.Println(time.Now().Format(".99")) // 13
fmt.Println(time.Now().Format(".9")) // 1
Enter fullscreen mode Exit fullscreen mode

Time Zone Location

format: MST

fmt.Println(time.Now().Format("MST")) // WIB, UTC, +04, -07
Enter fullscreen mode Exit fullscreen mode

Time Zone Offset

format: Z07, -07

fmt.Println(time.Now().Format("Z07")) // +07, Z, +02, -07
fmt.Println(time.Now().Format("-07")) // +07, +00, +02, -07
Enter fullscreen mode Exit fullscreen mode

Example

fmt.Println(time.Now())
// 1993-09-10 13:37:11.000000011 +0700 GMT+7

"2006-01-02 15:04:05.999999999 -0700 MST" 
// 1993-09-10 13:37:11.000000011 +0700 GMT+7

"2006-01-02T15:04:05Z07:00"
// 1993-09-10T13:37:00+07:00

"2006-01-02T15:04:05.999Z" 
// 1993-09-10T13:37:11Z 

"Monday, 02 January 2006 MST. 15:04:05.999." 
// Friday, 10 September 1993 WIB. 13:37:11.993.

Enter fullscreen mode Exit fullscreen mode

Top comments (0)