how to check the current time is within the given range in time type fields?

Sirisha Navudu2
Giga Expert

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.

find_real_file.png

1 ACCEPTED SOLUTION

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);
}

View solution in original post

7 REPLIES 7

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);
}

 

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);
}

Hi,

It is helpful, Thankyou.

Sirisha.