0

I want to validate date within 2 months between a given date using two(2) datepicker

example: DATEPICKERdatefrom = 02/05/2013 and DATEPICKERdateto = 03/08/2013 (dd/mm/yyyy)

what should be my statement?

if ( /*DATEPICKERdatefrom between DATEPICKERdateto is not between 2 months*/ )
{
messagebox.show("the Date must within 2 months")
}
else
{
//GO
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Awtszs
  • 323
  • 1
  • 8
  • 33
  • 1
    Simply a matter of crating new `DateTime` instance of your interest and compare it with `>` and `<` operators. – Sriram Sakthivel Sep 12 '14 at 08:29
  • Hi, StackOverflow is a site for when you have a problem with existing code. Questions requesting code to be written for you tend to not be well recieved. – dav_i Sep 12 '14 at 08:30

2 Answers2

0

here is an example how you can offset the date.

DateTime twoMonthsBack = DateTime.Now.AddMonths(-2);
DateTime twoMonthsLater = DateTime.Now.AddMonths(2);

you may replace DateTime.Now in the example with your date. and you can choose to validate the same.

perhaps

if(DATEPICKERdateto > DATEPICKERdatefrom.AddMonths(2))
{
    //to date is more than two months from start date
    messagebox.show("the Date must within 2 months");
}
else
{
    //GO
}

above example is based on assumption that from date is always less then to date.

pushpraj
  • 13,458
  • 3
  • 33
  • 50
0

If these values are DateTime, you can simply use < or > operators to compare.

if((date > DATEPICKERdatefrom) && (date < DATEPICKERdateto))

These operators are overloaded by DateTime as;

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator < (DateTime t1, DateTime t2) {
        return t1.InternalTicks < t2.InternalTicks;
}


[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool operator > (DateTime t1, DateTime t2) {
        return t1.InternalTicks > t2.InternalTicks;
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364