Checking if a date falls between a range of dates

Pradeep44
Kilo Contributor

How we can check  if a date falls between a range of dates i.e.,  1st Sep 2020 to 1st Aug 2025  in Server side scripting

4 REPLIES 4

Murthy Ch
Giga Sage

Hi Pradeep,

You can try with below logic and enhance it as per you requirements. Just tested in Background scripts

var currentDate= new GlideDate(); //comparing with todays date
gs.info(typeof currentDate +currentDate);
if(currentDate >= '2020-09-01'&& currentDate <= '2025-08-01') //1st Sep 2020 to 1st Aug 2025
{
gs.info('Current date is falls between a range of dates');
}
else
{
gs.info('Not in a range');
}

Hope it helps

 

Thanks

Murthy

Thanks,
Murthy

shloke04
Kilo Patron

Hi,

Please follow the steps below to achieve your requirement:

var createdDate = new GlideDateTime(<createdTime>); // Replace your First Date Field

var updatedTime = new GlideDateTime(<updatedTime>); // Replace your Second Date Field

var startDate = new GlideDateTime(<startDate>); // Replace the date which you want to validate

if(startDate.getNumericValue() < updatedTime.getNumericValue() && startDate.getNumericValue() > createdDate.getNumericValue()){

// within //Place your logic here if it falls within the date range

}

else{

// outside date

}

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

junwei_li
Kilo Contributor

var startDate = new GlideDateTime('2020-09-01').getLocalDate();

var endDate = new GlideDateTime('2025-08-01').getLocalDate();

var item= new GlideDateTime(checkDate).getLocalDate();

// startDate <= item <= endDate

if(startDate .onOrBefore(item) && endDate.onOrAfter(item)) {

    // TODO

} else {

    // TODO

}

Hajar BENJAHHAR
Mega Sage

Hello, 

var start_date = new GlideDateTime("2020-09-01");
var end_date = new GlideDateTime("2025-08-01");
var selected_date = new GlideDateTime("2024-01-01");
if(selected_date.compareTo(start_date)==1 && selected_date.compareTo(end_date)==-1){
    gs.info("date in the range");
}else{
    gs.info("not in the range");
}

Best regards, 

Hajar