Checking if a date falls between a range of dates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2021 08:34 PM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2021 10:01 PM
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
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2022 12:21 AM
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
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2022 01:22 AM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2022 10:13 AM
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