如何解决使用date.Hour C#进行登录和注销的更正检测
在设置aftternoon登录时间时遇到问题,该时间应为12:31 pm至4:59 pm
当我尝试使用下午登录名更改时间时,它会给我not recorded
else语句
DateTime date = DateTime.Now;
// 0:00 to 11:59
if (date.Hour >= 0 && date.Hour <= 11) {
MessageBox.Show("Morning Login");
}
// 12:00 between 0 minutes and 30
else if (date.Hour == 12 && date.Minute >= 0 && date.Minute <= 30) {
MessageBox.Show("Morning logout");
}
// This is the condition I'm having problems with
else if (date.Hour == 12 || date.Hour >= 13 && date.Hour <= 16) {
// I'm setting the time to afternoon login that should be 12:31pm to 4:59pm
MessageBox.Show("Afternoon Login");
}
else if (date.Hour >= 17 && date.Hour <= 23)
{
MessageBox.Show("Afternoon logout");
}
else
{
MessageBox.Show("Not recorded");
}
解决方法
正如Panagiotis在评论中建议的那样,您应该使用TimeSpan
进行比较
您的下午检查条件可能是
else if ((date.TimeOfDay >= new TimeSpan(12,31,0)) && (date.TimeOfDay <= new TimeSpan(16,59,0)))
{
MessageBox.Show("Afternoon Login");
}
引用this
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。