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

Hitoshi Ozawa
Giga Sage
Giga Sage

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

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

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.

 

 

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.