The solution I provide below has 4 combo boxes as you explained in your question. In this solution, I am first converting the time to 24 hours format(depending on "AM" or "PM" is selected) and then comparing both the inputs.
The below is a button click event which will check the hours difference,
private void btnCheckTime_Click(object sender, EventArgs e)
{
DateTime iStartTime = Get24Hours(cmbStartTime.Text, cmbStartampm.Text);
DateTime iEndTime = Get24Hours(cmbEndTime.Text, cmbEndampm.Text);
TimeSpan time_difference = iEndTime - iStartTime;
int hours_diff=Convert.ToInt16(time_difference.Hours);
if (hours_diff != 2)
{
}
else {
}
}
This function will convert the input hours to 24 hour format. So, always the values of hours are between 0-24.
private DateTime Get24Hours(string time, string ampm)
{
DateTime TheTime = Convert.ToDateTime(time);
if (ampm.Equals("PM"))
{
DateTime TheTimePM = TheTime.AddHours(12);
return TheTimePM;
}
return TheTime;
}
Try this solution and let me know if you have any difficulties.
thank you.