Golang time.Time.After() 函数的使用及实例
在 Go 语言中,time 包提供了确定和查看时间的功能。在 Go 语言中, Time.After() 函数用于检查所述时间点 t 是否在所述 u 之后。此外,此函数在 time 包中定义。在此,您需要导入“time”包才能使用这些函数。
语法:
func (t Time) After(u Time) bool
在此处,“t”是声明的时间,“u”是作为 After() 方法中的参数存在的时间。
返回值: 如果“t”在“u”之后,则返回 true,否则返回 false。
示例1:
// Golang program to illustrate the usage of
// Time.After() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Declaring t and u in UTC
t := time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC)
u := time.Date(2019, 0, 0, 0, 0, 0, 0, time.UTC)
// Calling After method
res := t.After(u)
// Prints output
fmt.Printf("%v", res)
}
输出:
true
示例2:
// Golang program to illustrate the usage of
// Time.After() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Declaring t and u in UTC
t := time.Date(2030, 0, 0, 0, 0, 0, 0, time.UTC)
u := time.Date(2040, 0, 0, 0, 0, 0, 0, time.UTC)
// Calling After method
res := t.After(u)
// Prints output
fmt.Printf("%v", res)
}
输出:
false