Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
Dear All,

I am trying to compare two date hours as below:
DateTime Login =Convert.ToDateTime(ds.Tables[1].Rows[0]["Login"]);
      DateTime Logout = Convert.ToDateTime(ds.Tables[1].Rows[0]["LogOut"]);
      int var = DateTime.Compare(Login,Logout);
      if (var > 3 )
      {
       lblCount.Text = "0";
      }

Here, I am actually checking if the difference between login and Logout is more than 3 hours then :
lblCount.Text = "0";

I don't think its correct. Can any one please suggest me how to compare the dates for 3 hours?
Posted
Updated 14-Jun-11 19:09pm
v3
Comments
Raj.rcr 15-Jun-11 1:25am    
Login and Logout times are actually getting today's date and time when the user logs in or off.. How to use Hour property for these two?
Prerak Patel 15-Jun-11 1:57am    
Did you try - int var = Logout.Subtract(Login).Hours;
You can post a comment what you get when you tried the solution.
Posting a comment to your own question won't help. No one would know that you made a comment here.
Raj.rcr 15-Jun-11 2:00am    
yeah.. I have tried as below:
int var = Logout.Subtract(Login).Hours;
if (var > 3 )
{
lblCount.Text = "0";
}

But the problem is: Even if the condition fails, its still making lblCount as "0".
Prerak Patel 15-Jun-11 2:48am    
Try putting else part as well.
if (var > 3 )
{
lblCount.Text = "0";
}
else
{
lblCount.Text = "";
}
parmar_punit 15-Jun-11 4:29am    
for that see my solution it will more helpful to you

Use int var = Logout.Subtract(Login).Hours;


Ref:http://www.victorchen.info/get-date-difference-in-c/[^]
 
Share this answer
 
v2
DateTime.Compare is used to check if the 2 DateTime parameters are equal or not. For this one, you might want to consider using the Hour property for your scenario. A high level approach would be to subtract Logout.Hour to Login.Hour and the check if the result is greater than 3. Refer here[^] for details.
 
Share this answer
 
here the solution which u want...the string value which I taken that value like you taken from the datatable.....
string INDate = "2011-02-02 4:45:25";
string OutDate = "2011-02-02 12:33:33";
DateTime startDate = Convert.ToDateTime(INDate);
DateTime endDate = Convert.ToDateTime(OutDate);
TimeSpan diff = endDate.Subtract(startDate);
string FV = diff.TotalHours.ToString();

Best of Luck.....
 
Share this answer
 
v2
It will help you......
DateTime Login = Convert.ToDateTime(ds.Tables[1].Rows[0]["Login"]);
Login.AddHours(3); // add 3 hours to the login time so comparison will be preety simple.
DateTime Logout = Convert.ToDateTime(ds.Tables[1].Rows[0]["LogOut"]);
int var = DateTime.Compare(Login, Logout); //if login is greater time then, it takes less then 3 hours...
if (var < 0)
{
    lblCount.Text = "0";
}
 
Share this answer
 
Compare won't give you the desired result. Rather use the "Hour" property, but remember the "Hour" property will not consider the day component.
 
Share this answer
 
v2
try this

http://stackoverflow.com/questions/807909/how-can-i-compare-time-in-sql-server
 
Share this answer
 
Use TimeSpan with the Hours properties to get the time difference.
That is always better.

For an exmaple, see here[^].
 
Share this answer
 

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