在 Go 中,可以使用 time.Time 类型的 Equal() 方法来判断两个日期是否相等。

示例代码:

package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Date(2021, 7, 1, 0, 0, 0, 0, time.UTC)
    t2 := time.Date(2021, 7, 1, 12, 0, 0, 0, time.UTC)

    if t1.Equal(t2) {
        fmt.Println("t1 and t2 are equal")
    } else {
        fmt.Println("t1 and t2 are not equal")
    }
}

输出:

t1 and t2 are not equal

在上面的示例中,我们创建了两个 time.Time 类型的变量 t1t2,分别表示 2021 年 7 月 1 日的零点和中午 12 点。然后使用 Equal() 方法判断它们是否相等,由于它们的时间部分不同,因此输出结果为“t1 and t2 are not equal”。

golang time.time 判断日期相等

原文地址: https://www.cveoy.top/t/topic/uWO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录