Time Field Comparison
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2022 08:06 PM
Hello All,
We have a requirement to compare current time with 2 time (not date/time) fields of custom table. If the current time is in between the above two time fields. We have used following logic which is mostly working fine but sometime not working fine for system time zone. Could anyone help me with this?
Logic used:
var currentTime = new GlideDateTime().getLocalTime().getDisplayValueInternal(); // Get Current Local Time
var begTime = new GlideDateTime(grHRAssociate.u_beginning_time).getLocalTime().getDisplayValueInternal();
var endTime = new GlideDateTime(grHRAssociate.u_end_time).getLocalTime().getDisplayValueInternal();
if ( currentTime >= begTime && currentTime <= endTime &&) {
agents.push(grHRAssociate.u_hr_associate.toString());
}
Thank you!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2022 06:46 AM
Tested with the following script to make sure the gtStart and gtEnd are different.
Test script:
var grHRAssociate= new GlideRecord('u_datetest_table1');
if (grHRAssociate.get('<test sys_id>')) {
var gtCurrent = new GlideDateTime();
var startTime = grHRAssociate.u_beginning_time;
var endTime = grHRAssociate.u_end_time;
startTime = startTime.split(' ')[1];
endTime = endTime.split(' ')[1];
gs.info('current:' + gtCurrent + ' startTime:' + startTime + ' endTime:' + endTime);
var gtStart = new GlideDateTime(gtCurrent.getDate() + ' ' + startTime);
var gtEnd = new GlideDateTime(gtCurrent.getDate() + ' ' + endTime);
gs.info('gtStart:' + gtStart + ' gtEnd:' + gtEnd);
gs.info('numericValue() : gtCurrent:' + gtCurrent.getNumericValue() + ' gtStart:' + gtStart.getNumericValue() + ' gtEnd:' + gtEnd.getNumericValue());
}
Execution result:
*** Script: current:2022-03-12 14:45:45 startTime:00:00:00 endTime:09:00:00
*** Script: gtStart:2022-03-12 00:00:00 gtEnd:2022-03-12 09:00:00
*** Script: numericValue() : gtCurrent:1647096345354 gtStart:1647043200000 gtEnd:1647075600000

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2022 06:09 PM
Megha, my bad. I thought the u_end_time would return a String in hh:mm:ss format but it seems to be of type GlideDate.
In this case, .getTime() is returning a date/time in format "1970-01-01 hh:mm:ss". Since there is a date part (i.e "1970-01-01", the script was calculating the time incorrectly).
I've fixed the script to use <GlideDateTime>.add(<GlideTime>).
var gdtCurrent = new GlideDateTime(); // set to current datetime (in UTC)
var gdtStart = new GlideDateTime(gs.beginningOfToday()); // initialize to today midnight
var gdtEnd = new GlideDateTime(gs.beginningOfToday()); // initialize to today midnight
var gtStart = new GlideTime();
gtStart.setValue(grHRAssociate.u_beginning_time.getDisplayValue()); // set GlideTime to start time value from the table
gdtStart.add(gtStart); // add GlideDate to current midnight date
var gtEnd = new GlideTime();
gtEnd.setValue(grHRAssociate.u_end_time.getDisplayValue()); // set GlideTime to end time value from the table
gdtEnd.add(gtEnd); // add GlideDate to current midnight date
if (gdtCurrent.getNumericValue() >= gdtStart.getNumericValue() && gdtCurrent.getNumericValue() <= gdtEnd.getNumericValue()) {
agents.push(grHRAssociate.u_hr_associate.toString());
}
Tested the logic of the script using the following script.
var grHRAssociate= new GlideRecord('<user table>');
if (grHRAssociate.get('<sys_id of a record>')) {
var gdtCurrent = new GlideDateTime();
gdtCurrent.setDisplayValueInternal("<datetime in local timezone>"); // e.g. "2022-03-13 23:15:00"
var gdtStart = new GlideDateTime(gs.beginningOfToday());
var gdtEnd = new GlideDateTime(gs.beginningOfToday());
gs.info("gdtStart:" + gdtStart.getDisplayValue() + " gdtEnd:" + gdtEnd.getDisplayValue() + " current:" + gdtCurrent.getDisplayValue());
var gtStart = new GlideTime();
gtStart.setValue(grHRAssociate.u_beginning_time.getDisplayValue());
gdtStart.add(gtStart);
var gtEnd = new GlideTime();
gtEnd.setValue(grHRAssociate.u_end_time.getDisplayValue());
gdtEnd.add(gtEnd);
gs.info("start:" + gdtStart.getDisplayValue() + " end:" + gdtEnd.getDisplayValue() + " current:" + gdtCurrent.getDisplayValue());
gs.info('numericValue(). gdtCurrent :' + gdtCurrent .getNumericValue() + ' gdtStart:' + gdtStart.getNumericValue() + ' gdtEnd:' + gdtEnd.getNumericValue());
if (gdtCurrent.getNumericValue() >= gdtStart.getNumericValue() && gdtCurrent.getNumericValue() <= gdtEnd.getNumericValue()) {
//agents.push(grHRAssociate.u_hr_associate.toString());
gs.info("OK");
} else {
gs.info("NG");
}
}
Sample execution result. beginning time:9:00, end time:23:30, current time:23:15 -> OK
*** Script: gdtStart:2022-03-13 00:00:00 gdtEnd:2022-03-13 00:00:00 current:2022-03-13 23:15:00
*** Script: start:2022-03-13 09:00:00 end:2022-03-13 23:30:00 current:2022-03-13 23:15:00
*** Script: numericValue(). gdtCurrent :1647180900000 gdtStart:1647129600000 gdtEnd:1647181800000
*** Script: OK