time.Time.Before() Function in Golang With Examples
Last Updated :
19 Apr, 2020
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Before() function in Go language is used to check if the stated time instant t is before the stated u or not. 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) Before(u Time) bool
Here, “t” is the stated time and “u” is the time that is present as an argument in the Before() method.
Return Value: It returns true if “t” is present before “u” else it returns false.
Example 1:
package main
import "fmt"
import "time"
func main() {
t := time .Date(2019, 0, 0, 0, 0, 0, 0, time .UTC)
u := time .Date(2020, 0, 0, 0, 0, 0, 0, time .UTC)
res := t.Before(u)
fmt.Printf( "%v" , res)
}
|
Output:
true
Example 2:
package main
import "fmt"
import "time"
func main() {
t := time .Date(2030, 0, 0, 0, 0, 0, 0, time .UTC)
u := time .Date(2025, 0, 0, 0, 0, 0, 0, time .UTC)
res := t.Before(u)
fmt.Printf( "%v" , res)
}
|
Output:
false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...