Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please can anyone help me for the following?

I want to calculate time span in 9:00 AM to 11:00 AM and then checking this interval time span

I chose First Combo box in 9:00, Second Combo box in AM or PM, Third Combo box in 11:00 and Fourth Combo Box in AM or PM. and then using string.Format("{0} {1} {2} {3} {4}", FirstCombo.Text, SecondCombo.Text, "To", ThirdCombo.Text, FourthCombo.Text)

Resut are 9:00 AM To 11:00 AM

I will check this time interval are always including in 2 hours.
Posted
Updated 12-Sep-12 19:13pm
v2

Hi ,
Check this
To calculate the time
C#
DateTime dt = Convert.ToDateTime("9:00");
            DateTime dt2 = Convert.ToDateTime("11:00");

            TimeSpan ts = dt2 - dt;
            MessageBox.Show( ts.TotalMinutes.ToString());

http://www.dotnetspider.com/resources/458-How-find-difference-between-two-Dates-C-or.aspx[^]
http://msdn.microsoft.com/en-us/library/aa287590%28v=vs.71%29.aspx[^]
Best Regards
M.Mitwalli
 
Share this answer
 
Comments
__TR__ 13-Sep-12 13:08pm    
+5
Mohamed Mitwalli 13-Sep-12 14:02pm    
Thanks TR :)
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)
       {
           //Get the start time and convert to int (int 24 hours)
           DateTime iStartTime = Get24Hours(cmbStartTime.Text, cmbStartampm.Text);
           //Get the end time and convert to int (in 24 hours)
           DateTime iEndTime = Get24Hours(cmbEndTime.Text, cmbEndampm.Text);

           //Find the difference
           TimeSpan time_difference = iEndTime - iStartTime;
           int hours_diff=Convert.ToInt16(time_difference.Hours);

           //Now check for 2 hours
           if (hours_diff != 2)
           {
               //this is not 2 hours. Not valid
           }

           else {
               //This is 2 hours. Valid
           }
       }


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.
 
Share this answer
 
Comments
hein thu 13-Sep-12 3:36am    
Than you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900