golang 基于时间比较 加上时区
golang 东八区时间字符串比较先转换为时间戳
timezone := time.FixedZone("CST", 8*3600)
start, end := "2022-06-09 21:17:47", "2022-06-19 10:55:34"
startTime, err := time.ParseInLocation(formatTime, start, timezone)
if err != nil {
panic(err)
}
endTime, _ := time.ParseInLocation(formatTime, end, timezone)
if err != nil {
panic(err)
}
nowTimestamp := time.Now().In(timezone).Unix()
startTimestamp := startTime.Unix()
endTimestamp := endTime.Unix()
然后基于时间戳比较
fmt.Printf(" startTimestamp : %d \n endTimestamp : %d \n nowTimestamp : %d\n", startTimestamp, endTimestamp, nowTimestamp)
if nowTimestamp < startTimestamp {
fmt.Println(" 预约会议......")
}
if startTimestamp <= nowTimestamp && nowTimestamp <= endTimestamp {
fmt.Println("进行中.......")
}
if endTimestamp < nowTimestamp {
fmt.Println("历史会议.....")
}
自带的after brofre
if startTime.After(nowTime) {
fmt.Println(" 预约会议......")
}
if nowTime.After(startTime) && nowTime.Before(endTime) {
fmt.Println("进行中.......")
}
if endTime.Before(nowTime) {
fmt.Println("历史会议.....")
}
if startTime.After(nowTime) || (nowTime.After(startTime) && nowTime.Before(endTime)) {
fmt.Println(" 非历史会议......")
}
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/16365417.html