Click here to Skip to main content
15,914,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

how to check and compare both date and Month and year and finally only come output DeactivationDate is greter than ActivationDate that only come on output.
C#
string strCon = GetConnectionString();
          OleDbConnection objCon = new OleDbConnection(strCon);
          string objcmd = "select TblBill.SlNo,TblBillNew.EmployeeNo,TblBill.EmployeeName,TblBill.CardNo,TblBill.DepartmentName,left(TblBill.DeactivationDate,10)as DeactivationDate,TblBill.DeactivationTime from TblBill LEFT JOIN TblBillNew ON TblBill.CardNo=TblBillNew.CardNo where  TblBill.DeactivationDate <>'' And TblBill.DeactivationDate > TblBill.ActivationDate;";
          objCon.Open();
          OleDbDataAdapter objAdpter = new OleDbDataAdapter(objcmd, objCon);
          DataTable dt = new DataTable();
          objAdpter.Fill(dt);
          objCon.Close();
          return dt;
Posted
Updated 28-Sep-13 3:19am
v2

The answer is: don't.
Comparing strings always works in string order:
1
10
11
...
2
20
21
...
Which is generally pretty useless for any practical purpose.

Instead, store your dates as Date or DateTime fields - that way you can compare them directly, enter only valid values, and return them as dates to your external code for formatting as the user wants: you don't need to look at it each time and compare what it might be against the users currently preferred display settings.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Sep-13 10:40am    
Of course, a 5.
—SA
You can use a TimeSpan object to store the subtraction of a date with another. TimeSpan gives the output in integer format.

C#
TimeSpan ts=new TimeSpan();
DateTime dt1=Convert.ToDateTime(date1);
DateTime dt2=Convert.ToDateTime(date2);

ts=dt1.Subtract(dt2);//Result in integer format
if(ts>0)
{
Response.Write("dt1 is greater than dt2");
}
else
{
Response.Write("dt1 is less than or equal to dt2");
}


-Anurag
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Sep-13 10:40am    
Up-voted with 4. It's better to use DateTime.Parse/ParseExact/TryParse/TryParseExact
Also, your statement "dt1 is lesser than dt2" is plain wrong. Correct one would be "less or equal".
Will you please fix it, as it is important?
—SA
Anurag Sinha V 28-Sep-13 10:46am    
done
Sergey Alexandrovich Kryukov 28-Sep-13 11:13am    
Great.
—SA
Sharathkumar k.r 30-Sep-13 7:52am    
thank u.. that code worked.
Anurag Sinha V 30-Sep-13 7:55am    
np

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