Open In App

time.Now() Function in Golang With Examples

Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, time packages supplies functionality for determining as well as viewing time. The Now() function in Go language is used to find the current local time. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.

Syntax:

func Now() Time

Return Value: It returns the current local time.

Example 1:




// Golang program to illustrate the usage of
// time.Now() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Calling Now() method
    tm := time.Now()
  
    // Prints current 
    // local time in UTC
    fmt.Printf("%s", tm)
}


Output:

2020-04-09 11:24:14.785868848 +0000 UTC m=+0.000187421

Here, current time is returned in UTC.

Example 2:




// Golang program to illustrate the usage of
// time.Now() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Calling Now() method
    tm := time.Now()
    tm1 := time.Now()
  
    // Prints current local time in UTC
    fmt.Printf("%s\n", tm)
  
    // Sleep for 10 seconds
    time.Sleep(10 * time.Second)
  
    // Prints the current time after 10
    // seconds of the sleep is over
    fmt.Printf("%s", tm1)
}


Output:

2020-04-09 11:37:59.087484568 +0000 UTC m=+0.000106559
2020-04-09 11:37:59.087484779 +0000 UTC m=+0.000106736


Last Updated : 21 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads