Menu
Coddy logo textTech

Getting the Current Time

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

When working with dates and times in your Go programs, you'll need to use Go's built-in time package. This package provides all the functionality you need to work with time-related operations.

The most common starting point is getting the current time. Go makes this simple with the time.Now() function:

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    fmt.Println(now)
}

The time.Now() function returns a time.Time object that represents the current moment in time. When you print this object, it displays the date and time in a readable format, including the timezone information.

This time.Time object contains all the information about that specific moment - the year, month, day, hour, minute, second, and even nanoseconds. You'll use this as the foundation for all other time operations in Go.

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 time tracking application that captures the current time and displays it in multiple formats. This challenge will test your ability to use the time.Now() function and work with time.Time objects to extract different components of the current moment.

You will receive one input:

  • A string containing display format requests separated by commas in the format "format1,format2,format3" (e.g., "full,date,time,year,month,day")

Your task is to:

  1. Use time.Now() to capture the current time and store it in a variable
  2. Display the application header: "=== TIME TRACKER APPLICATION ==="
  3. Display the timestamp capture confirmation: "Current time captured successfully"
  4. Parse the input by splitting on commas to get individual format requests
  5. Display the formats header: "Displaying time in requested formats:"
  6. For each format request, display the time information according to the specified format:
    • For "full": Display "Full timestamp: [complete_time_object]"
    • For "date": Display "Date only: [year]-[month]-[day]" (use the Date() method components)
    • For "time": Display "Time only: [hour]:[minute]:[second]" (use Hour(), Minute(), Second() methods)
    • For "year": Display "Year: [year_value]" (use Year() method)
    • For "month": Display "Month: [month_name]" (use Month() method)
    • For "day": Display "Day: [day_value]" (use Day() method)
    • For "weekday": Display "Weekday: [weekday_name]" (use Weekday() method)
    • For "unix": Display "Unix timestamp: [unix_seconds]" (use Unix() method)
  7. Count and display statistics:
    • "=== TIME INFORMATION SUMMARY ==="
    • "Total formats displayed: [number_of_formats]"
    • "Timestamp components extracted: [number_of_unique_components]"
  8. Display additional time details:
    • "Additional time details:"
    • "- Nanoseconds: [nanosecond_value]" (use Nanosecond() method)
    • "- Location: [timezone_location]" (use Location() method)
    • "- Is zero time: [true_or_false]" (use IsZero() method)
  9. Display the completion message: "Time tracking completed - all requested formats processed"

Use the time package to work with time objects, the strings package to split the input string, and the fmt package for formatted output. Remember that time.Now() returns a time.Time object that contains all the information about the current moment, and you can extract specific components using the various methods available on this object. This challenge demonstrates how to capture the current time and extract different pieces of information from a single time.Time object.

Cheat sheet

To work with dates and times in Go, import the time package:

import "time"

Get the current time using time.Now():

now := time.Now()
fmt.Println(now)

The time.Now() function returns a time.Time object representing the current moment, including year, month, day, hour, minute, second, nanoseconds, and timezone information.

Extract specific components from a time.Time object using methods:

now := time.Now()
year := now.Year()
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
nanosecond := now.Nanosecond()
weekday := now.Weekday()
unix := now.Unix()
location := now.Location()
isZero := now.IsZero()

Try it yourself

package main

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

func main() {
	// Read input
	var formatRequests string
	fmt.Scanln(&formatRequests)
	
	// Use a fixed time for consistent output (Go's reference time)
	// Instead of time.Now(), use time.Date for predictable results
	currentTime := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)
	
	// Split format requests
	formats := strings.Split(formatRequests, ",")
	
	// TODO: Write your code below
	// Display application header
	// Display timestamp capture confirmation
	// Display formats header
	// Process each format request and display accordingly
	// Use a map to track unique components for counting
	// Use proper formatting (e.g., %02d for zero-padded numbers)
	// Display summary statistics
	// Display additional time details
	// 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