The time.Now() is a built-in function in Go (Golang) that returns the current local time. The function returns a time.Time value represents the current point in time, including the current date and time down to nanosecond precision.
Syntax
func Now() Time
Return value
It returns the current local time.
Example 1
To use the time.Now() function, you need to import the time package first.
package main
import (
"fmt"
"time"
)
func main() {
currentTime := time.Now()
fmt.Println("The current time is:", currentTime)
}
Output
The current time is: 2023-03-17 19:37:10.097827 +0530 IST m=+0.000182710
In this example, we import the time package and then call time.Now() to get the current time. The result is stored in the currentTime variable, which is then printed to the console.
Example 2
To get the year, month, day, hour, minute, second, and nanosecond individually in Golang, you can use the time.Now() function.
package main
import (
"fmt"
"time"
)
func main() {
currentTime := time.Now()
fmt.Println("Year:", currentTime.Year())
fmt.Println("Month:", currentTime.Month())
fmt.Println("Day:", currentTime.Day())
fmt.Println("Hour:", currentTime.Hour())
fmt.Println("Minute:", currentTime.Minute())
fmt.Println("Second:", currentTime.Second())
fmt.Println("Nanosecond:", currentTime.Nanosecond())
}
Output
Year: 2023
Month: March
Day: 17
Hour: 19
Minute: 42
Second: 57
Nanosecond: 70569000
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.