C# .NET - How to compare date-time in c#? - Asked By Sudhansu shekhar on 14-Jun-12 02:31 AM



 Hi all,

 This is an Urgent. Could u please help me out.  How to compare date time in c#, I have two record in DB i.e. From_time(10 am) and To_Time(2 pm). Now i'm trying to find compare between 10am(small value) and 2 pm(Greater value), my code is as follow:-

DataTable dt = dc.DataTable("select max(scheduledTime) from tMEDAPPS_SheduleDetails where doctorId='" + id + "' and scheduledDate='" +  avlDate+ "'");
            DataRow dr=dt.Rows[0];
            if (dr[0].ToString() == "")
                dt.Rows.Clear();
            double minute = 10.0;
            if (dt.Rows.Count == 0)
            {
                DateTime toDateTime = Convert.ToDateTime(toTime);
                //string time = dc.getTime(Convert.ToDateTime(dr[0]));
                DateTime dtime = Convert.ToDateTime(fTime);
               // dtime = dtime.AddMinutes(minute);
               if (DateTime.Compare(dtime,toDateTime) < 0)
             //   if()
                    return dc.getTime(dtime);
                else
                {
                    MessageBox.Show("Appointment Time Must Be Within Doctor Avialable Time", "Doctor Not Avialable", MessageBoxButton.OK, MessageBoxImage.Information);
                    txtDocName.Text = "";
                    txtApptDate.Text = "";
                    txtApptTime.Text = "";
                }

Thanks.
Chintan Vaghela replied to Sudhansu shekhar on 14-Jun-12 03:13 AM

Hi Frndz,

 

Functionality: Compare AM/PM time

 

 

First Convert time in 24 hours Format using Convert.ToDateTime(FromTime).ToString("Hmm")

 

After then Convert to Integer Convert.ToInt32(FromTime)

 

After then check FromTime with ToTime

 

Full Logic:

 

string FromTime = "10 am";

        FromTime = Convert.ToDateTime(FromTime).ToString("Hmm");

        string ToTime = "2 pm";

        ToTime = Convert.ToDateTime(ToTime).ToString("Hmm");

 

        string Message = "";

        if (Convert.ToInt32(FromTime) == Convert.ToInt32(ToTime))

        {

          Message = "Same Time";

        }

        else if (Convert.ToInt32(FromTime) < Convert.ToInt32(ToTime))

        {

          Message = "To Time is Greate than From Time";

        }

        else

        {

          Message = "From Time is Greate than To Time";

        }

 

 

Hope this helpful!

Thanks

 

 

 

Jitendra Faye replied to Sudhansu shekhar on 14-Jun-12 09:44 AM
Try like this-

if  ( Convert.ToDateTime(dtime)== Convert.ToDateTime(toDateTime))
  {
     //Your code
}
[)ia6l0 iii replied to Sudhansu shekhar on 14-Jun-12 11:10 AM
An easier and a fool proof solution would be to use the Hour property of the DateTime object. System.DateTime.Now.Hour is what you should be targeting.

Seeing your code, I see that dr[0] contains the datetime field. In this case, after you convert take the hour part from the datetime object. 
DateTime scheduledTime = Convert.ToDateTime(dr[0]);
int scheduledHour = scheduledTime.Hour;
if (scheduledHour < 10 && scheduledHour > 14)
{
MessageBox.Show("Appointment Time Must Be Within Doctor Avialable Time", "Doctor Not Avialable", MessageBoxButton.OK, MessageBoxImage.Information 
}
else
{
}

Hope this helps.