BudiBadu Logo

Samplebadu

Code with Example
BudiBadu Logo
Samplebadu

Go by Example: Time Formatting

Go 1.23

Go uses a unique reference time (Mon Jan 2 15:04:05 MST 2006) for formatting. This example shows how to format dates and times using standard layouts and custom patterns.

Code

package main

import (
    "fmt"
    "time"
)

func main() {
    p := fmt.Println

    // 1. Get current time
    t := time.Now()
    p("Current:", t)

    // 2. RFC3339 (Standard for APIs)
    // "2006-01-02T15:04:05Z07:00"
    p("RFC3339:", t.Format(time.RFC3339))

    // 3. Custom Layouts
    // The reference time is: Mon Jan 2 15:04:05 MST 2006
    // (01/02 03:04:05PM '06 -0700)
    
    // Format: 3:04PM
    p("Kitchen:", t.Format(time.Kitchen))
    
    // Format: YYYY-MM-DD
    p("Date Only:", t.Format("2006-01-02"))
    
    // Format: Weekday, Month Day, Year
    p("Long Date:", t.Format("Monday, January 2, 2006"))
    
    // Format: Day/Month/Year Hour:Minute
    p("Custom:", t.Format("02/01/2006 15:04"))

    // 4. Formatting with numeric components
    fmt.Printf("Year: %d, Month: %d, Day: %d\n", t.Year(), t.Month(), t.Day())
}

Explanation

Time formatting in Go deviates from the strftime pattern found in C-derived languages, opting instead for a mnemonic-based reference time system that uses the specific timestamp Mon Jan 2 15:04:05 MST 2006 as the layout template. To format a time.Time value, developers construct a string containing components of this reference date—such as 2006 for the year, 01 for the month, or 15 for the hour—which the Format method then replaces with the corresponding values from the actual time object. This design choice, while initially idiosyncratic, eliminates the need to memorize abstract specifiers like %Y or %m, allowing developers to visualize the output format directly within the code.

The standard library includes a comprehensive set of predefined layout constants, such as time.RFC3339 and time.UnixDate, which cover common serialization standards and ensure consistency across applications. When defining custom layouts, precision is paramount; using 15 denotes 24-hour time while 3 denotes 12-hour time, and the distinction between Jan (abbreviated month) and January (full month name) allows for granular control over the textual representation. This system provides a type-safe and highly readable mechanism for converting internal time representations into human-readable strings or machine-parsable formats required for protocols and logging.

  • Reference Time: Mon Jan 2 15:04:05 MST 2006.
  • Mnemonic: 1 (month), 2 (day), 3 (hour), 4 (minute), 5 (second), 6 (year).
  • Constants: Use time.RFC3339 for JSON/APIs.
  • Precision: Use .000 for milliseconds or .999 for variable precision.

Code Breakdown

17
Using a standard constant. 'time.RFC3339' is the most common format for web APIs (e.g., "2023-10-27T10:00:00Z"). Using constants avoids typos in the layout string.
24
The Kitchen format. 'time.Kitchen' is "3:04PM". It demonstrates how Go handles 12-hour clocks with AM/PM suffixes automatically.
27
Custom date layout. We use "2006-01-02". Note that we must use exactly these numbers. If we used "2023-11-11", Go would not understand it as a layout.
33
Mixing components. "02/01/2006 15:04" tells Go to put the day first (02), then month (01), then year (2006), followed by 24-hour time (15).