-2
我比较时间。看我有这个代码在C#中比较AM和PM
DateTime t1 = DateTime.Now;
DateTime StartMorningCleaning = Convert.ToDateTime("6:00:00 AM");
DateTime EndMorningCleaning = Convert.ToDateTime("6:59:59 AM");
DateTime StartMorning = Convert.ToDateTime("7:00:00 AM");
DateTime EndMorning = Convert.ToDateTime("6:59:59 PM");
DateTime StartNightCleaning = Convert.ToDateTime("7:00:00 PM");
DateTime EndNightCleaning = Convert.ToDateTime("7:59:59 PM");
DateTime StartNight = Convert.ToDateTime("8:00:00 PM");
DateTime BeforeMidnight = Convert.ToDateTime("11:59:59 PM");
DateTime Midnight = Convert.ToDateTime("12:00:00 AM");
DateTime EndNight = Convert.ToDateTime("5:59:59 AM");
我试图比较当前时间,如果它在之间的开始和结束时间。问题是,当我去下午部分它不起作用,我不知道为什么。从StarNightCleaning直到EndNight。任何想法为什么?这里是我的代码在比较部分
if (t1 >= StartMorningCleaning && t1 <= EndMorningCleaning)
{
MessageBox.Show("Morning Cleaning Time");
CleaningTime = false;
}
else if (t1 >= StartMorning && t1 <= EndMorning)
{
MessageBox.Show("MorningTour");
CleaningTime = true;
}
else if (t1 >= StartNightCleaning && t1 <= EndNightCleaning)
{
MessageBox.Show("Night Cleaning Time");
CleaningTime = false;
}
else if (t1 >= StartNight && t1 <= BeforeMidnight)
{
MessageBox.Show("NightTour");
CleaningTime = true;
}
else if (t1 >= Midnight && t1 <= EndNight)
{
MessageBox.Show("NightTour");
CleaningTime = true;
}
定义“不起作用”。当你在调试器中完成这一步时,具体发生了什么?什么是't1'? – David
什么都没有发生。从字面上看。没有显示错误。工作的唯一部分是“If(t1> = StartMorningCleaning && t1 <= EndMorningCleaning) MessageBox.Show(”Morning Cleaning Time“); CleaningTime = false; } else if(t1> = StartMorning && t1 <= EndMorning) { MessageBox.Show(“MorningTour”); CleaningTime = true; “其余的不会工作。 T1是我系统的当前时间 –
“没有任何反应,字面意思。”意味着应用程序根本没有运行。在这种情况下代码不是问题,因为代码永远不会到达。 – David