Options
- 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