Golang time.Time.IsZero() Golang函数及实例
在Go语言中,time包提供了用于确定和查看时间的功能。 Time.IsZero() 函数用于检查指定时间“t”是否表示零时刻,即公元1年1月1日00:00:00 UTC。此外,此功能在time包中定义。 您需要导入“time”包以使用这些函数。
语法:
func (t Time) IsZero() bool
在这里,“t”是指定的时间。
返回值: 如果指定的时间表示零时刻,则返回true,否则返回false。
实例1:
// Golang程序列举了对
// Time.IsZero()函数的用法
// 包括主程序
package main
// 导入fmt和time
import "fmt"
import "time"
// 调用主程序
func main() {
// 定义IsZero方法的t参数
t := time.Date(0001, 1, 1, 00, 00, 00, 00, time.UTC)
// 调用IsZero方法
IsZeroOrnot := t.IsZero()
// 打印输出
fmt.Printf("%v\n", IsZeroOrnot)
}
输出:
true
实例2:
// Golang程序列举了对
// Time.IsZero()函数的用法
// 包括主程序
package main
// 导入fmt和time
import "fmt"
import "time"
// 调用主程序
func main() {
// 定义IsZero方法的t参数
t := time.Date(0001, 2, 1, 00,
00, 00, 00, time.UTC)
// 调用IsZero方法
IsZeroOrnot := t.IsZero()
// 打印输出
fmt.Printf("%v\n", IsZeroOrnot)
}
输出:
false