How to compare dates in a GlideRecord query

Shane18
Kilo Expert

Hi, I'm trying to compare Date fields (not date/time, just date fields). I want it to only get me records where the start dates are after the current end date, or where the end dates are after the current start date. Basically, to ensure no other bookings occur on these dates. Code below:

var gr_getBookings = new GlideRecord('x_210272_insta_ava_instance_booking');
gr_getBookings.addQuery('name_of_instance', current.name_of_instance);
//Verify there are no bookings where the start date is before the current end date, or where the end date is after the current start date.
gr_getBookings.addQuery('start_date', "<=", current.end_date).addOrCondition('end_date', '>=', current.start_date);

gr_getBookings.query();
while(gr_getBookings.next()) {

//do this

}

However, the while loop always runs, even if there are no records within the date range?

10 REPLIES 10

Anurag Tripathi
Mega Patron
Mega Patron

Put this in gr.addEncodedQuery('start_date<=javascript:gs.daysAgoEnd(0)^end_date>=javascript:gs.daysAgoStart(0)')

-Anurag

The only issue is the current.start_date and current.end_date are values the user has input, so they'll be different every time.

yes, this is the field name, value will be picked up from whatever user has entered in these fields.

-Anurag

The user will enter a start date, e.g. Sept 10 2018, they will also enter an end date, e.g. Sept 21 2018.

I want to then compare this start and end date to all the other records, ensuring that none of them overlap, i.e. this isn't already booked for those dates.