golang 代码实现判断当前时间是否在"19:00"和"24:00"这两个时间范围内

发布时间 2023-09-24 22:48:18作者: Lucky小黄人^_^

代码是 chatgpt 写的

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	// 获取今天的年月日
	year, month, day := now.Date()

	// 创建两个新的时间点,它们在今天的 19:00 和 24:00
	startTime, _ := time.Parse("2006-01-02 15:04", fmt.Sprintf("%d-%02d-%02d 19:00", year, month, day))
	endTime, _ := time.Parse("2006-01-02 15:04", fmt.Sprintf("%d-%02d-%02d 24:00", year, month, day))

	// 判断当前时间是否在这两个时间点之间
	if now.After(startTime) && now.Before(endTime) {
		fmt.Println("Current time is between 19:00 and 24:00")
	} else {
		fmt.Println("Current time is not between 19:00 and 24:00")
	}
}