Menu
Coddy logo textTech

Formatting Time

Part of the Logic & Flow section of Coddy's GO journey — lesson 60 of 68.

Once you have a time.Time object, you'll often need to convert it into a readable string format. Go uses a unique approach to time formatting that's different from other programming languages.

Instead of using format codes like %Y-%m-%d, Go uses a reference date as a template. This reference date is Mon Jan 2 15:04:05 MST 2006, which represents the exact moment: January 2nd, 2006, at 3:04:05 PM Mountain Standard Time.

now := time.Now()
formatted := now.Format("2006-01-02")
fmt.Println(formatted) // Output: 2023-10-26

The key insight is that you arrange the reference date components in the pattern you want. For a YYYY-MM-DD format, you use "2006-01-02" because that's how the reference date looks in that pattern. For a different format like Jan 2, 2006, you'd use "Jan 2, 2006".

This layout-based approach makes Go's time formatting both memorable and flexible. Once you remember the reference date, you can create any format by simply arranging its components in your desired pattern.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a multi-format date and time display system that formats time.Time objects using Go's unique reference date layout system. This challenge will test your ability to use the Format method with different layout patterns to create various date and time representations.

You will receive two inputs:

  • A string containing date information in the format "year:month:day:hour:minute" (e.g., "2023:12:25:14:30")
  • A string containing format requests separated by commas in the format "format1,format2,format3" (e.g., "iso,american,european,time12,time24")

Your task is to:

  1. Parse the first input by splitting on colons to extract year, month, day, hour, and minute values
  2. Convert these string values to integers using the strconv package
  3. Create a time.Time object using time.Date with the parsed values (use time.UTC for location, 0 for seconds and nanoseconds)
  4. Display the application header: "=== DATE/TIME FORMATTER ==="
  5. Display the source time: "Source time: [complete_time_object]"
  6. Parse the second input by splitting on commas to get format requests
  7. Display the formatting header: "Formatted outputs:"
  8. For each format request, use the appropriate layout string with the Format method:
    • For "iso": Use layout "2006-01-02" and display "ISO format: [formatted_result]"
    • For "american": Use layout "01/02/2006" and display "American format: [formatted_result]"
    • For "european": Use layout "02/01/2006" and display "European format: [formatted_result]"
    • For "long": Use layout "January 2, 2006" and display "Long format: [formatted_result]"
    • For "time12": Use layout "3:04 PM" and display "12-hour time: [formatted_result]"
    • For "time24": Use layout "15:04" and display "24-hour time: [formatted_result]"
    • For "full": Use layout "Monday, January 2, 2006 at 3:04 PM" and display "Full format: [formatted_result]"
    • For "compact": Use layout "20060102" and display "Compact format: [formatted_result]"
  9. Calculate and display statistics:
    • "=== FORMATTING SUMMARY ==="
    • "Total formats applied: [number_of_format_requests]"
    • "Date components: [year]-[month]-[day]"
    • "Time components: [hour]:[minute]"
  10. Display reference information:
    • "=== REFERENCE DATE INFO ==="
    • "Go uses reference date: Mon Jan 2 15:04:05 MST 2006"
    • "This represents: January 2nd, 2006 at 3:04:05 PM"
    • "Layout patterns arrange these components as needed"
  11. Display the completion message: "Date formatting completed using Go's layout system"

Use the time package for time operations, the strings package to split input strings, the strconv package to convert strings to integers, and the fmt package for formatted output. Remember that Go's time formatting uses the reference date Mon Jan 2 15:04:05 MST 2006 as a template, and you arrange these components in the pattern you want. This challenge demonstrates how Go's unique layout-based formatting system works by having you apply multiple different format patterns to the same time object.

Cheat sheet

Go uses a unique reference date approach for time formatting: Mon Jan 2 15:04:05 MST 2006

To format a time.Time object, use the Format method with layout patterns based on the reference date:

now := time.Now()
formatted := now.Format("2006-01-02")
fmt.Println(formatted) // Output: 2023-10-26

Common layout patterns:

  • "2006-01-02" - ISO format (YYYY-MM-DD)
  • "01/02/2006" - American format (MM/DD/YYYY)
  • "02/01/2006" - European format (DD/MM/YYYY)
  • "January 2, 2006" - Long format
  • "3:04 PM" - 12-hour time
  • "15:04" - 24-hour time
  • "Monday, January 2, 2006 at 3:04 PM" - Full format
  • "20060102" - Compact format

The key is to arrange the reference date components in your desired pattern.

Try it yourself

package main

import (
	"fmt"
	"strconv"
	"strings"
	"time"
)

func main() {
	// Read input
	var dateInput string
	var formatInput string
	fmt.Scanln(&dateInput)
	fmt.Scanln(&formatInput)
	
	// Parse date components by splitting on colons
	dateComponents := strings.Split(dateInput, ":")
	
	// Convert string components to integers
	year, _ := strconv.Atoi(dateComponents[0])
	month, _ := strconv.Atoi(dateComponents[1])
	day, _ := strconv.Atoi(dateComponents[2])
	hour, _ := strconv.Atoi(dateComponents[3])
	minute, _ := strconv.Atoi(dateComponents[4])
	
	// Create time.Time object
	sourceTime := time.Date(year, time.Month(month), day, hour, minute, 0, 0, time.UTC)
	
	// Parse format requests
	formatRequests := strings.Split(formatInput, ",")
	
	// TODO: Write your code below
	// Display the application header
	// Display the source time
	// Process each format request and display formatted outputs
	// Calculate and display statistics
	// Display reference information
	// Display completion message
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow