当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Golang time.Time.Before()用法及代码示例


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

用法:

func (t Time) Before(u Time) bool

在此,“t”是规定的时间,“u”是在Before()方法中作为自变量出现的时间。

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

范例1:



// Golang program to illustrate the usage of 
// Time.Before() 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(2019, 0, 0, 0, 0, 0, 0, time.UTC) 
    u:= time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC) 
  
    // Calling Before method 
    res:= t.Before(u) 
  
    // Prints output 
    fmt.Printf("%v", res) 
}

输出:

true

范例2:

// Golang program to illustrate the usage of 
// Time.Before() 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(2025, 0, 0, 0, 0, 0, 0, time.UTC) 
  
    // Calling Before method 
    res:= t.Before(u) 
  
    // Prints output 
    fmt.Printf("%v", res) 
}

输出:

false



相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 time.Time.Before() Function in Golang With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。