- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2022 11:55 PM
Hi,
we have created time field , as start and end time, so how to check whether the current time is within the time given in the these start and end range. Please provide any solution.
Thanks & Regards,
Sirisha.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2022 09:48 AM
Try the following.
function checkIfWithinTime() {
var startTime = (new GlideDateTime(current.u_start_time)).getLocalTime();
var endTime = (new GlideDateTime(current.u_end_time)).getLocalTime();
var curTime = (new GlideDateTime()).getLocalTime();
return (curTime >= startTime && curTime <= endTime);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2022 12:49 AM
Hi Sirisha,
Is this to run on the server-side?
If so, try the following script. If it is a business rule, replace to use current.u_start_time and current.u_end_time.
function checkIfWithinTime() {
var gr = new GlideRecord('<table containing time fields>');
if (gr.get('<sys_id of field to check>')) {
var now = new GlideDateTime();
var curTime = now.getDisplayValue().split(' ')[1];
var startTime = gr.getDisplayValue('u_start_time'); // replace with start_time field name
var endTime = gr.getDisplayValue('u_end_time'); // replace with end_time field name
if (curTime >= startTime && curTime <= endTime) {
return true;
} else {
return false;
}
}
}
var result = checkIfWithinTime();
if (result) {
gs.info("within time");
} else {
gs.info("not within time");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2022 12:52 AM
If business rule, something like below.
function checkIfWithinTime() {
var now = new GlideDateTime();
var curTime = now.getDisplayValue().split(' ')[1];
return (curTime >= current.u_start_time && curTime <= current.u_end_time);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2022 05:09 AM
we are using the start time and end time in 24hours format. But glide date time value is in 12 hours format. can you provide a solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2022 05:12 AM
we are using the start time and end time in 24hours format. But glide date time value is in 12 hours format. can you provide a solution.