golang

How to Use time.Now() Function in Golang – Examples

Go programming language offers several built-in functions to handle time and among these functions, the time.Now() is the widely used one. It allows you to retrieve the current local time in your system time zone. The function returns a Time struct, which includes the current date and time. After that, you can perform several operations on the struct to get your desired output.

Follow this tutorial to learn the use of time.Now() function in Go with some basic examples for better understanding.

How to Use time.Now() Function in Golang – Examples

Before moving towards using the time.Now() function in Go programming, you must learn the basic syntax for this function, which is given below:

func Now() Time

The time.Now() function takes no arguments and returns the current local time as a Time value. You can use this value to perform a variety of time-related operations in your code, such as formatting the time or calculating the time elapsed between two events.

Note: The time.Now() will rely on the system clock of the machine running your code, so you must ensure that the system clock is set correctly to get accurate time measurements.

Step By Step Implementation of time.Now() Function in Golang

Here is a step-by-step implementation to use time.Now() function in Go:

Step 1: First import the time package before starting the main function.

Step 2: Then call the time.Now() function within the main function and stores the current value of time in a variable.

Step 3: Then print the variable value to get the desired time.

The below-written example code illustrates the working of time.Now() function in Go:

package main

import "fmt"

import "time"

func main() {

   time := time.Now()

   fmt.Printf("The Current time is: %s", time)

}

In the above code, we have used the time.Now() function to find the current date and time and then stored it in the time variable. Next, we used the fmt.Printf() function to print the time variable to the console.

Output

The time.Now() returns the value of the time in the local time zone; here it returns the time in UTC as the current time zone of my server is UTC. In the above output, you will also see the m= value which is a monotonic clock that is used to measure the differences in time to check any changes in the date and time of the device.

You can also add a duration to the current time in Go using the time.Now() function and the code for such a scenario are given below:

package main

import "fmt"

import "time"

func main() {

   now := time.Now()

   future := now.Add(24 * time.Hour)

   fmt.Println("Future time:", future)

}

The above code retrieves the current time using the time.Now() function, then adds 24 hours to it using the Add() method of the Time type. After that, it prints the final output to the console via the fmt.Println() function.

Output

You can also use the time.Now() function to separately identify the current year, month, day, hours, minutes, and seconds.

The following code is used for the previous scenario.

package main

import (

   "fmt"

   "time"

)

func main() {

   Time := time.Now()

   fmt.Println("Current time is:", Time)

   fmt.Println("Year is:", Time.Year())

   fmt.Println("Month is:", Time.Month())

   fmt.Println("Day is:", Time.Day())

   fmt.Println("Hour is:", Time.Hour())

   fmt.Println("Minute is:", Time.Hour())

   fmt.Println("Second is:", Time.Second())

}

The above program extracts and prints the current year, month, day, hour, minute, and second by calling the relevant functions on the Time object returned by time.Now() function.

Output

Bottom Line

The time-related operations are essential in most programming languages, and Golang is no exception. The time.Now() function is a powerful built-in function that can be used to retrieve the current local time and perform a variety of time-related operations in Go. By following the step-by-step implementation provided in this guide, you can easily use this function to retrieve the current time or add duration to it, or extract specific time components.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.