+ Start a Discussion
jucuzoglujucuzoglu 

How do you check if a date falls within a range of two dates?

How do you take a date value and determine if it falls within two other date values?

Best Answer chosen by Admin (Salesforce Developers) 
stcforcestcforce

Ran a quick test using the following in anonymous block. Seemed okay.

 

string year = '2008';

string month = '10';

string day = '5'; string day2 = '6';

string hour = '12';

string minute = '20';

string second = '20';

string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;

string stringDate2 = year + '-' + month + '-' + day2 + ' ' + hour + ':' + minute + ':' + second;

Date myDate = date.valueOf(stringDate);

Date myDate2 = date.valueOf(stringDate2);

System.debug(''+(myDate>myDate2));

System.debug(''+(myDate<myDate2));

System.debug(''+(myDate==myDate2));

 

hence

if(date1<testdate&& testdate<date2)

 

 

 

All Answers

ElcalvoMikeElcalvoMike

you could try

integer dates = firstdate.daysbetween(seconddate)

integer datetotest= thirddate.daysbetween(seconddate)

 

if (datetotest>dates){

//you have a date between two dates

}

stcforcestcforce

I'm not 100% sure but try using <,>,==, with your dates. It's definitely supported for datetimes. Create some dates in an anonymous block and see whether they respond as you would expect.

stcforcestcforce

Ran a quick test using the following in anonymous block. Seemed okay.

 

string year = '2008';

string month = '10';

string day = '5'; string day2 = '6';

string hour = '12';

string minute = '20';

string second = '20';

string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;

string stringDate2 = year + '-' + month + '-' + day2 + ' ' + hour + ':' + minute + ':' + second;

Date myDate = date.valueOf(stringDate);

Date myDate2 = date.valueOf(stringDate2);

System.debug(''+(myDate>myDate2));

System.debug(''+(myDate<myDate2));

System.debug(''+(myDate==myDate2));

 

hence

if(date1<testdate&& testdate<date2)

 

 

 

This was selected as the best answer
jucuzoglujucuzoglu

Thanks for the response. I did the following (borrowing heavily from your example) and it works like a charm

 

Date myDate = date.valueOf('2004-6-6');
Date startDate = date.valueOf('2000-5-5');
Date endDate = date.valueOf('2004-6-6');

System.Debug(myDate >= startDate && myDate <= endDate);