问题描述
在设置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