Time Comparison in c#

Posted by Amritha444 under ASP.NET on 10/27/2010 | Points: 10 | Views : 8203 | Status : [Member] | Replies : 4
I want to compare two time formats like 08:30:00 ,I retrieve valus from backend my sql
string Hrin =dtod.Rows[i]["M_Intime"].tostring();
string Hrein =dtod.Rows[i]["M_Intime"].ToString();
iwant to compare these two values but this operation is not possible.In database values of the type Time .i want to compare hrin>=hrein how is this possible.Any fit conversions exist.Please Suggest a good idea.




Responses

Posted by: Prasham on: 10/27/2010 [Member] Starter | Points: 25

Up
0
Down
Hello,

I didnt tried this but please check once.

One of the option is I guess convert the values to DateTime instance and then DateTime class has the Compare() like DateTime.Compare(objDatetime1 , objDatetime2). This method returns integer value indicating whether first value is greater than or less than or equal with the second.

You can convert string to DateTime like

DateTime objDatetime1 = Convert.ToDateTime(Hrin);
DateTime objDatetime2= Convert.ToDateTime(Hrein);

Hope these details will help you.

Thanks and Regards
Prasham

Thanks and Best Regards
Prasham

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tdhivamca on: 10/27/2010 [Member] Starter | Points: 25

Up
0
Down
Hi ,

you can try like below code

string Hrin = DateTime.Parse(dtod.Rows["M_Intime"]).ToString("t");

string Hrein = DateTime.Parse(dtod.Rows["M_Intime"]).ToString("t");

if (DateTime.Compare(DateTime.Parse (Hrin ), DateTime.Parse (Hrein )) >= 0 )
{
// your code....
}


-
Dhivakaran T

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 10/27/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Try the following code,

DateTime Hrin = Convert.ToDateTime(dtod.Rows[1]["M_Intime"].ToString());

DateTime Hrein = Convert.ToDateTime(dtod.Rows[2]["M_Intime"].ToString());
if(Hrin >= Hrein )
{
// Code
}


Note: dtod.Rows[1]["M_Intime"].Tostring() --> its comes in DateTime format means above code possible else just convert into DateTime format and then check the condition.

Cheers :)

Thanks,
T.Saravanan

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lalji_mer on: 10/27/2010 [Member] Bronze | Points: 25

Up
0
Down
Hi,
i this link will help you.about string comparison difference way!
http://www.netframeworkdev.com/net-base-class-library/best-way-for-string-comparison-in-c-10055.shtml

LP MER

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response