Golang time.Time.Before()的用法与示例

Golang time.Time.Before()的用法与示例

在Go语言中,时间包提供了确定和查看时间的功能。在Go语言中, Time.Before() 函数用于检查指定的时间点t是否早于指定的u。此外,此函数在time包中定义。在这里,您需要导入“time”包以使用这些函数。

语法:

func (t Time) Before(u Time) bool

这里,“t”是指定的时间,“u”是作为Before()方法参数存在的时间。

返回值: 如果“t”在“u”之前返回true,否则返回false。

示例1:

// Golang程序演示Time.Before()函数的用法

// 包含主包
package main

// 导入fmt和time
import "fmt"
import "time"

// 调用主方法
func main() {

// 在UTC中声明t和u
    t:=time.Date(2019, 0, 0, 0, 0, 0, 0, time.UTC)
    u:=time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC)

// 调用Before方法
    res:=t.Before(u)

// 输出结果
    fmt.Printf("%v", res)
} 

输出:

true

示例2:

// Golang程序演示Time.Before()函数的用法

// 包含主包
package main

// 导入fmt和time
import "fmt"
import "time"

// 调用主方法
func main() {

// 在UTC中声明t和u
    t:=time.Date(2030, 0, 0, 0, 0, 0, 0, time.UTC)
    u:=time.Date(2025, 0, 0, 0, 0, 0, 0, time.UTC)

// 调用Before方法
    res:=t.Before(u)

// 输出结果
    fmt.Printf("%v", res)
} 

输出:

false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程