codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

33 / 72 topics
27Standard Library Overview28IO Package29Net Package30HTTP Package31JSON Package32XML Package33Time Package34OS Package35Fmt Package36Math Package37Regexp Package38Log Package39Flag Package40Context Package41Embed Package
Tutorials/Go (Golang)/Time Package
🐹Go (Golang)

Time Package

Updated 2026-04-20
2 min read

Time Package

The time package in Go is a fundamental part of the standard library, providing comprehensive functionality for working with time, including date and time manipulation, formatting, parsing, and more. This tutorial will cover various aspects of the time package, including its basic usage, advanced features, and best practices.

Introduction to Time Package

The time package in Go provides a rich set of functions and types to handle time-related operations. It is designed to be efficient, thread-safe, and easy to use. The primary type used in this package is the Time, which represents an instant in time with nanosecond precision.

Basic Usage

Getting Current Time

To get the current local time, you can use the Now() function:

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    fmt.Println("Current local time:", now)
}

Formatting Time

The Format method allows you to format a Time value according to a specified layout string. The layout strings follow the reference time Mon Jan 2 15:04:05 MST 2006.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    formattedTime := now.Format("2006-01-02 15:04:05")
    fmt.Println("Formatted time:", formattedTime)
}

Parsing Time

The Parse function allows you to parse a string into a Time value based on a specified layout.

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "2006-01-02 15:04:05"
    parsedTime, err := time.Parse(layout, "2023-09-15 14:30:00")
    if err != nil {
        fmt.Println("Error parsing time:", err)
        return
    }
    fmt.Println("Parsed time:", parsedTime)
}

Advanced Features

Time Zones

Go supports multiple time zones, and you can work with them using the Location type.

package main

import (
    "fmt"
    "time"
)

func main() {
    loc, err := time.LoadLocation("America/New_York")
    if err != nil {
        fmt.Println("Error loading location:", err)
        return
    }
    now := time.Now().In(loc)
    fmt.Println("Current time in New York:", now)
}

Time Durations

The Duration type represents a length of time. It can be used to add or subtract durations from a Time.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    oneHourLater := now.Add(time.Hour)
    fmt.Println("One hour later:", oneHourLater)
}

Time Ranges and Intervals

You can use the After, Before, and Equal methods to compare times.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    futureTime := now.Add(time.Hour)
    if futureTime.After(now) {
        fmt.Println("Future time is after current time")
    }
}

Best Practices

  1. Use time.Time for Time Representation: Always use the time.Time type to represent times in your application. Avoid using plain integers or strings.

  2. Handle Time Zones Correctly: Be aware of time zones and handle them appropriately, especially when dealing with user input or external data.

  3. Error Handling: Always check errors returned by functions like Parse and LoadLocation.

  4. Use Constants for Layouts: Define constants for your layout strings to avoid typos and make your code more readable.

  5. Avoid Mutable Time Values: The time.Time type is immutable, so you don't need to worry about concurrent modifications.

Conclusion

The time package in Go provides a robust set of tools for handling time-related operations. By understanding its basic usage and advanced features, you can effectively manage dates and times in your applications. Always follow best practices to ensure accurate and efficient time handling.


PreviousXML PackageNext OS Package

Recommended Gear

XML PackageOS Package