Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

GlideDateTime() | Script not Working

chatsaurav19
Tera Contributor

Hi All,

 

Any idea why the loop within is not showing the appropriate result?

 

var holiday1 = new GlideDateTime('2024-05-01 00:00:00');
var holiday2 = new GlideDateTime('2024-05-02 00:00:00');
var holiday3 = new GlideDateTime('2024-05-03 00:00:00');

var startDate = new GlideDateTime('2024-04-29 00:00:00');
var endDate = new GlideDateTime('2024-05-06 00:00:00');

while (endDate >= startDate) {
       gs.info("Inside Loop 1..."); //Working
       if((startDate.getDate() == holiday1.getDate()) || (startDate.getDate() == holiday2.getDate()) || (startDate.getDate() == holiday3.getDate())){
            gs.info("Inside Loop 2...."); //Not Working
        }
    startDate.addDays(1);    
}


Output:::

*** Script: Inside Loop 1...
*** Script: Inside Loop 1...
*** Script: Inside Loop 1...
*** Script: Inside Loop 1...
*** Script: Inside Loop 1...
*** Script: Inside Loop 1...
*** Script: Inside Loop 1...
*** Script: Inside Loop 1...


 Regards,

Saurabh

1 ACCEPTED SOLUTION

Dhananjay Pawar
Kilo Sage

Hi,

Use below updated code.

 

var holiday1 = new GlideDateTime('2024-04-29 00:00:00');
var holiday2 = new GlideDateTime('2024-05-02 00:00:00');
var holiday3 = new GlideDateTime('2024-05-03 00:00:00');

var startDate = new GlideDateTime('2024-04-29 00:00:00');
var endDate = new GlideDateTime('2024-05-06 00:00:00');
while (endDate >= startDate) {
       gs.print("Inside Loop 1..."); //Working
       if((startDate.getDate().toString() == holiday1.getDate().toString()) || (startDate.getDate().toString() == holiday2.getDate().toString()) || (startDate.getDate().toString() == holiday3.getDate().toString())){
            gs.print("Inside Loop 2...."); //Not Working
        }
    startDate.addDays(1);    
}
 
Thanks.

View solution in original post

1 REPLY 1

Dhananjay Pawar
Kilo Sage

Hi,

Use below updated code.

 

var holiday1 = new GlideDateTime('2024-04-29 00:00:00');
var holiday2 = new GlideDateTime('2024-05-02 00:00:00');
var holiday3 = new GlideDateTime('2024-05-03 00:00:00');

var startDate = new GlideDateTime('2024-04-29 00:00:00');
var endDate = new GlideDateTime('2024-05-06 00:00:00');
while (endDate >= startDate) {
       gs.print("Inside Loop 1..."); //Working
       if((startDate.getDate().toString() == holiday1.getDate().toString()) || (startDate.getDate().toString() == holiday2.getDate().toString()) || (startDate.getDate().toString() == holiday3.getDate().toString())){
            gs.print("Inside Loop 2...."); //Not Working
        }
    startDate.addDays(1);    
}
 
Thanks.