Im comparing time. See I have this code
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");
Im trying to compare the current time if its in between Start and end times. The problem is when i go the the PM part it doesnt work and i have no idea why. Starting from StarNightCleaning up to EndNight. Any ideas why? here is my code in the comparing section
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;
}
Use Hour property and try reduce logic to numbers.
int t1 = DateTime.Now.Hour;
if (t1 >= 6 && t1 < 7) {
MessageBox.Show("Morning Cleaning Time");
CleaningTime = false;
} else if (t1 >= 7 t1 < 19) {
MessageBox.Show("MorningTour");
CleaningTime = true;
} else if (t1 >= 19 && t1 < 20) { //PM
MessageBox.Show("Night Cleaning Time");
CleaningTime = false;
} else if (t1 >= 20 && t1 <= 23) { //PM
MessageBox.Show("NightTour");
CleaningTime = true;
} else if (t1 >= 0 && t1 <= 5) { //AM - this if can be just else (remove if part)
MessageBox.Show("NightTour");
CleaningTime = true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.