Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
EmilyCobb

# Date range comparison without a year

I have a table of date ranges split out by day and month, and I need to determine if a user provided date falls within the range.

I have no years in the table. I was using this logic:
```if(qRec.Expected_Start_Date__c >= Date.newInstance(qRec.Expected_Start_Date__c.year(), Integer.valueOf(soqlResults.Start_Month__c), Integer.valueOf(soqlResults.Start_Day__c)) &&
qRec.Expected_Start_Date__c <= Date.newInstance(qRec.Expected_Start_Date__c.year(), Integer.valueOf(soqlResults.End_Month__c), Integer.valueOf(solqResults.End_Day__c))){```
Which works for the majority of the use cases, until it crosses a year and needs to fall into the 12/25 - 1/24 range. I'd prefer to take year out of the comparison all together, as it is not relevant. Any suggestions appreciated.
Thanks

Marcelo Costa
I would not convert it to date for comparing... I would use a formula to calculate that using Integers (Date is no more than a long anyways...)
I probably would create a static method in a Service class a little bit like the following:
```//Arguments would be provided date and one of your objects that I'm calling QRec.
public static boolean isProvidedDateWithinRange(qRec record, Date provideDate){

Boolean isCrossYear = false;
Integer iniciald;
Integer finald;

//Tests if is cross year.
if(record.End_Month__c<record.Start_Month__c){
isCrossYear=true;
}

iniciald = 100*Integer.valueOf(record.Start_Month__c) + Integer.valueOf(record.Start_Day__c);
finald = 100*Integer.valueOf(record.End_Month__c) + Integer.valueOf(record.End_Day__c);

//just makes sure that final is the right number.
if(isCrossYear){
finald = final+1200;
}

//Now you have an initial and final values.
userDate =  100*provideDate.month()+provideDate.day;

//This will adjust in case the provided date is following year
if(isCrossYear && userDate<inicial){
userDate=userDate+1200;
}
//All exceptions checked, we can just finishthecheck and return
if(userDate > iniciald && userDate < finald){
return true;
}
return false;

```
Can be simplified a little... I have not tested the code, and is full of comments, but it is just to give an Idea...
Good Luck :)