Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..
please tell me how can i compare the dates in the if condition..... m i doing it in the right way ?????

i m fetching the due_date from the database for comparision....

if the due_date is 2 days bigger then i want to perform some task... else something else

thanks



C#
string str1 = "select due_date from mConfirm where emp_id='" + s + "'";
           sda = new SqlDataAdapter(str1, cn);
           ds = new DataSet();
           sda.Fill(ds);

           string d = ds.Tables[0].Rows[0].ItemArray[0].ToString();
           if (Convert.ToDateTime(d) >= DateTime.Today)    // HERE WHAT SHOULD I DO
           {
               LblAlreadyExists.Text = "Already Under Process";
           }
           else
           {
               LblAlreadyExists.ForeColor = System.Drawing.Color.Red;
               LblAlreadyExists.Text = "RECORD ALREADY EXISTS";
               CHKConfirmed.Enabled = false;
               BtnConfirmd.Enabled = false;
           }
Posted

Try this:
C#
string d = ds.Tables[0].Rows[0].ItemArray[0].ToString();
DateTime dt = DateTime.Parse(d);
if (dt.AddDays(2) >= DateTime.Today)


This is just a demonstration. You can use AddDays, AddHours, etc. to make the code work for you. Also use TryParse instead of Parse.
 
Share this answer
 
I would suggest to use DateTime.TryParse() first to check if it is a valid date.
For adding 2 days to a DateTime you could use DateTime.Now.AddDays(2) and then compare the two dates in your condition.
Keep in mind that there might be some time values within the date when using DateTime.Now that you might want to get rid of first.
 
Share this answer
 
try this:-

string str1 = "select due_date from mConfirm where emp_id='" + s + "'";
           sda = new SqlDataAdapter(str1, cn);
           ds = new DataSet();
           sda.Fill(ds);
 
           string d = ds.Tables[0].Rows[0].ItemArray[0].ToString();
           if ((Convert.ToDateTime(d)- DateTime.Today)>=2)    // HERE WHAT SHOULD I DO
           {
               LblAlreadyExists.Text = "Already Under Process";
           }
           else
           {
               LblAlreadyExists.ForeColor = System.Drawing.Color.Red;
               LblAlreadyExists.Text = "RECORD ALREADY EXISTS";
               CHKConfirmed.Enabled = false;
               BtnConfirmd.Enabled = false;
           }

Convert.ToDateTime(d)- DateTime.Today

This subtracts two DateTime. The result is a TimeSpan object which has a Days property.

I hope this will work.
 
Share this answer
 
hi
try this
DateTime dt;
           DateTime dt2;
           int datecompare = DateTime.Compare(dt,dt2);
 
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