How to write if condition on the bases of time.

VIKAS MISHRA
Tera Contributor

I want to write a condition where if time is 8 AM to 10AM then new incidents assignment group should be "B" instead of "A".

Please suggest.

Need server side script.

4 REPLIES 4

Bhavya11
Kilo Patron

Hi @VIKAS MISHRA 

Try this 

var gdt = new GlideDateTime();
    var timeSection = gdt.toString().split(' ')[1]; //Gets the Time
    hour = timeSection.split(':')[0];
    // Check if the current time
    if (hour >= 8 && hour < 10) {
        current.assignment_group =gs.getProperty('sys_idofGroupA');
    } else {
        current.assignment_group = gs.getProperty('sys_idofGroupB');
    }

Please mark this response as correct or helpful if it assisted you with your question.

 

Thanks,

BK

In my system the current time is as below when i am running code gs.print(gs.nowDateTime()) in backgroud script

*** Script: 2024-07-25 11:36:19

 

And when i am writing below mentioned script in background script , its evertime printing "No" and never printing yes, please suggest.

I tried to print "hour" to chekc what value is coming in this variable found its showing 04, howvever i should print 11

 

 

 

var gdt = new GlideDateTime();
    var timeSection = gdt.toString().split(' ')[1]; //Gets the Time
    hour = timeSection.split(':')[0];
    // Check if the current time
    if (hour >= 8 && hour < 12) {
gs.print('yes');
    } else {
gs.print('no');
    }

 

 

Bhavya11
Kilo Patron

Hi @VIKAS MISHRA 

 

what's your time zone on the Service now User profile because when you run 
var gt = new GlideDateTime();  it will take that profile user time zone

so make your not checking you local time so that's why its printing no

 

 

Thanks,

BK

Bhavya11
Kilo Patron

@VIKAS MISHRA 

 

please try below code depending on your local timezone,i took IST i belong IST timezone.

 

var time = new GlideDateTime();
gs.info('GMT time is->' + time);
var targetTimezone = 'IST'; // ensure you give correct timezone Abbreviation
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone); 
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
gs.info('IST time is->' + time);


var timeSection = time.toString().split(' ')[1]; //Gets the Time
  var  hour = timeSection.split(':')[0];
    gs.print(hour)
    if (hour >= 8 && hour < 12) {
gs.print('yes');
    } else {
gs.print('no');
    }

Output:

Bhavya11_0-1721885217678.png

 

 

Please mark this response as correct or helpful if it assisted you with your question.

 

Thanks,

BK