gpt4 book ai didi

c# - 在时间段内激活

转载 作者:可可西里 更新时间:2023-11-01 10:44:37 28 4
gpt4 key购买 nike

我想知道有没有办法让我每天在一天中的某个时间显示一个消息框。例如

    if (DateTime >= 11:59)
{
messagebox.show("Good Morning");
}
else if (DateTime == 12:00 to 16:59)
{
messagebox.show("Good Afternoon");
}
else if (DateTime <= 17:00)
{
messagebox.show("Good Evening");
}

如果有办法做到这一点,请帮助我。谢谢。

最佳答案

您可以使用 TimeOfDay property DateTimeTimeSpan 值的比较;

var dt = DateTime.Now;
if (dt.TimeOfDay <= new TimeSpan(11, 59, 0))
{
MessageBox.Show("Good Morning");
}
else if (dt.TimeOfDay >= new TimeSpan(12, 0, 0) && dt.TimeOfDay < new TimeSpan(16, 59, 0))
{
MessageBox.Show("Good Afternoon");
}
else if (dt.TimeOfDay >= new TimeSpan(17, 0, 0))
{
MessageBox.Show("Good Evening");
}

关于c# - 在时间段内激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31355472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com