Open In App

time.Time.Equal() 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 Time.Equal() function in Go language is used to check if the stated times “t” and “u” represents identical time instant or not. And two times located in different locations can even be equal. 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 (t Time) Equal(u Time) bool

Here, “t” and “u” are the stated time.

Return Value: It returns true if the stated times are equal else it returns false.

Example 1:




// Golang program to illustrate the usage of
// Time.Equal() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t and u parameter of Equal method
    t := time.Date(2020, 3, 2, 12, 14, 0, 0, time.UTC)
    u := time.Date(2020, 3, 1, 36, 14, 0, 0, time.UTC)
  
    // Calling Equal method
    TimesequalOrnot := t.Equal(u)
  
    // Prints output
    fmt.Printf("%v\n", TimesequalOrnot)
}


Output:

true

Example 2:




// Golang program to illustrate the usage of
// Time.Equal() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t and u parameter 
    // of Equal method
    t := time.Date(2020, 3, 2, 12, 
              14, 07, 0, time.UTC)
      
    u := time.Date(2020, 3, 1, 36,
               14, 0, 0, time.UTC)
  
    // Calling Equal method
    TimesequalOrnot := t.Equal(u)
  
    // Prints output
    fmt.Printf("%v\n", TimesequalOrnot)
}


Output:

false


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