- 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 08:18 AM
ServiceNow keeps all datetime in a single format. It's probably the "local" display time in 12 hours format. So, instead of using .getDisplayValue(), use .getValue() to get the time in 24 hours format.
function checkIfWithinTime() {
var now = new GlideDateTime();
var curTime = now.getValue().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 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
‎05-02-2022 03:42 AM
Hi,
It is helpful, Thankyou.
Sirisha.