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

We have two text-box.

Now I want just compare not more then 3 months.

Question: date1 is not more then 3 months date2

I have found selected month like this.
C#
DateTime date1 = Convert.ToDateTime(TextBox1.Text);
     DateTime date2 = Convert.ToDateTime(TextBox2.Text);

     int mon1 = date1.Month;
     int mon2 = date2.Month;

Thanks,
Posted
Updated 24-Jul-12 23:47pm
v3

C#
DateTime date1 = Convert.ToDateTime(TextBox1.Text);
DateTime date2 = Convert.ToDateTime(TextBox2.Text);
       
int monthsDiff = (date1.Month - date2.Month) + 12 * (date1.Year - date2.Year);
 
Share this answer
 
I usally use the TimeSpan.FromXXX methods to do something like this:

C#
if((myDate - myOtherDate) > TimeSpan.FromSeconds(10))
{
   //Do something here
}


or

C#
//30 is days..
if (DateTime.Compare(date1, date2) < 30)
{
   //Do something here
}



--Amit
 
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