Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hello,

I have two time and i want to compare these two time and want to know which one is greater or smaller with AM or PM also.

if i have
10:15 AM and 2:00 PM

i use datetime and i convert that into string for taking as hh:mm tt

i use like
protected void Button1_Click(object sender, EventArgs e)
    {
        string s = "";
        s = drop_leave_hh.SelectedItem.Value + ":" + drop_leave_mm.SelectedItem.Value+""+drop_leave_time_dest.SelectedItem.Value;
       
        DateTime dt = System.Convert.ToDateTime(s);
        string leave_time = dt.ToString("hh:mm tt", System.Globalization.DateTimeFormatInfo.InvariantInfo);
        string nw = DateTime.Now.ToString("hh:mm tt", System.Globalization.DateTimeFormatInfo.InvariantInfo);
    }


so, i want to compare these two time.how can i achieve this.

Thanks in Advance...
Mitesh
Posted
Updated 19-Jul-12 19:13pm
v3

1 solution

C#
private void Form1_Load(object sender, EventArgs e)
{
         string strtime1 = "10:15 AM";
         DateTime time1 = Convert.ToDateTime(strtime1);
         string strtime2 = "2:00 PM";
         DateTime time2 = Convert.ToDateTime(strtime2);

         if (time1.TimeOfDay > time2.TimeOfDay)
            MessageBox.Show("Time1 is later than Time2");
         else
            MessageBox.Show("Time2 is later than Time1");
}
 
Share this answer
 
v2
Comments
[no name] 20-Jul-12 1:12am    
Dear SIr, when i use DateTime time2 = Convert.ToDateTime(strtime2) statement i convert as Datetime as Whole not only time.
JF2015 20-Jul-12 1:18am    
I modified my answer to only use the time part of the DateTime object for the comparison.
[no name] 20-Jul-12 1:40am    
not success beacuse if first time i set 10:00 Am and Second i set 2:00 PM so, it display first one is Large . that is not the wayyy
[no name] 20-Jul-12 2:26am    
string s = "";
s = drop_leave_hh.SelectedItem.Value + ":" + drop_leave_mm.SelectedItem.Value+""+drop_leave_time_dest.SelectedItem.Value;

DateTime dt = System.Convert.ToDateTime(s);
string leave_time = dt.ToString("hh:mm tt", System.Globalization.DateTimeFormatInfo.InvariantInfo);
string nw = DateTime.Now.ToString("hh:mm tt", System.Globalization.DateTimeFormatInfo.InvariantInfo);

DateTime now_date = Convert.ToDateTime(nw);
DateTime leave_time1 = Convert.ToDateTime(leave_time);

//now_date = now_date.ToString("HH:mm tt");
//leave_time1 = leave_time1.ToString("HH:mm tt");

if (leave_time1.TimeOfDay.Ticks > now_date.TimeOfDay.Ticks)
{
lbl_msg.Visible = true;
lbl_msg.Text = "Sorry";
}
else
{
lbl_msg.Visible = true;
lbl_msg.Text = "Success";
}

when i enter 12:00 Am so, it take Success when the System time is 11:45 Am because it take 12:00 AM as 00:00 Am.
JF2015 20-Jul-12 2:33am    
So, what is the issue? If you enter "12:00 AM" this is per definition earlier than "11:45 AM" or "11:45 PM" since on a 24 hour scale this is "0:00" in the morning. Therefore, the above code works for me.
See: http://en.wikipedia.org/wiki/12-hour_clock#Confusion_at_noon_and_midnight

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