← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 4 of 10

The time Package

The time package lets Go read the clock, measure how long things take, and work with dates the same way no matter where in the world your program runs.
Syntax
go
now := time.Now()
d := time.Duration(n) * time.Second
t.Format("2006-01-02")
time.Sleep(d)

Getting the Current Time

time.Now() returns a time.Time value representing the current moment, which carries a full date, time, and time zone, and supports formatting and arithmetic.

Example: Getting the Current Time

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"time"
)

// main is where the program starts running
func main() {
	// Short declaration: create now and infer the type
	now := time.Now()
	// Print the values followed by a newline
	fmt.Println("current year:", now.Year())
}

Measuring Elapsed Time

time.Since(start) returns a time.Duration representing how long has passed since a recorded start time -- the standard way to time how long an operation takes.

Example: Measuring Elapsed Time

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"time"
)

// main is where the program starts running
func main() {
	// Short declaration: create start and infer the type
	start := time.Now()
	// Short declaration: create sum and infer the type
	sum := 0
	// Loop: repeat while the condition holds
	for i := 0; i < 1000000; i++ {
		sum += i
	}
	elapsed := time.Since(start)
	// Print the values followed by a newline
	fmt.Println("sum:", sum, "took at least 0ns:", elapsed >= 0)
}

Working with Durations

A time.Duration represents a span of time and is usually constructed with constants like time.Second or time.Millisecond, which makes duration-based code, like sleeps and timeouts, self-documenting.

Note: Always build durations from the named constants (time.Second, etc.) instead of raw integers, for clarity.

Example: Working with Durations

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"time"
)

// main is where the program starts running
func main() {
	// Short declaration: create d and infer the type
	d := 2 * time.Second
	// Print the values followed by a newline
	fmt.Println("duration:", d)
	time.Sleep(1 * time.Millisecond)
	// Print the values followed by a newline
	fmt.Println("slept briefly")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Comparing two time.Time values with == when Sub/Before/After/Equal are more correct due to internal monotonic clock readings.
  2. Forgetting time.Sleep pauses the whole goroutine, blocking everything else it might be doing.
  3. Confusing a time.Duration's numeric value (nanoseconds) with a human unit, instead of using constants like time.Second for clarity.
Chapter Summary
  • time.Now returns the current local time as a time.Time value.
  • time.Duration represents a length of time, expressed with constants like time.Second or time.Millisecond.
  • time.Since computes how much time has elapsed since a given time.Time.
  • time.Sleep pauses the current goroutine for a given duration.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.