- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 10:43 AM
Hello all,
I'm trying to retrieve records from the cmn_schedule_span table where the start_date_time (a Schedule Date/Time type field) is greater than today's date.
Here’s a simplified version of the script I’m using for testing:
var gr = new GlideRecord('cmn_schedule_span');
gr.get('sys_id'); // Example sys_id for testing
var dateIS = gr.getDisplayValue('start_date_time');
gs.info('Display Value: ' + dateIS); // e.g., "05/19/2025 19:00:00"
var spanDate = new GlideDateTime(dateIS);
var numValueSpanDate = spanDate.getDate().getNumericValue();
gs.info('spanDate Numeric: ' + numValueSpanDate);
var today = new GlideDateTime();
var numValueToday = today.getDate().getNumericValue();
gs.info('today Numeric: ' + numValueToday);
if (spanDate > today) {
gs.info('Condition met: spanDate is after today');
}
Even when the numeric values of both dates are equal, the script still enters the if (spanDate > today) condition. I suspect this might be due to the Schedule Date/Time field type, possibly involving internal time zone handling or precision differences.
Has anyone else encountered this behavior?
Any advice on how to reliably compare a Schedule Date/Time field with the current date (ignoring time) would be appreciated.
Thanks in advance!
If you found my response helpful, please mark it as correct and close the thread to benefit others.
Regards,
Kaustubh
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 11:26 AM
Hi @kaustubh Desai ,
Option 1: Compare Only the Date Portion
If you're interested in just comparing dates (not times):
if (numValueSpanDate > numValueToday) {
gs.info('Condition met: spanDate is after today (by date only)');
}
Option 2: Truncate Time in GlideDateTime
If you must work with GlideDateTime, zero out the time portion to compare dates only:
function getStartOfDayGDT(gdt) {
var dateStr = gdt.getDate().toString(); // yyyy-MM-dd
return new GlideDateTime(dateStr + ' 00:00:00');
}
var truncatedToday = getStartOfDayGDT(today);
var truncatedSpan = getStartOfDayGDT(spanDate);
if (truncatedSpan.after(truncatedToday)) {
gs.info('spanDate is after today (date-only check)');
}
If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.
Regards,
Pratik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 11:26 AM
Hi @kaustubh Desai ,
Option 1: Compare Only the Date Portion
If you're interested in just comparing dates (not times):
if (numValueSpanDate > numValueToday) {
gs.info('Condition met: spanDate is after today (by date only)');
}
Option 2: Truncate Time in GlideDateTime
If you must work with GlideDateTime, zero out the time portion to compare dates only:
function getStartOfDayGDT(gdt) {
var dateStr = gdt.getDate().toString(); // yyyy-MM-dd
return new GlideDateTime(dateStr + ' 00:00:00');
}
var truncatedToday = getStartOfDayGDT(today);
var truncatedSpan = getStartOfDayGDT(spanDate);
if (truncatedSpan.after(truncatedToday)) {
gs.info('spanDate is after today (date-only check)');
}
If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.
Regards,
Pratik