Add 3 days to current date scoped app

Dhez Vu
Tera Expert

Hi, 

Anyone help me with the below scenario: 

I'm trying to add 3 days if the current system time is above 3 PM. Tried addDays and it's not working in the scoped application. 

Any suggestions ??

 

1 ACCEPTED SOLUTION

Hi,

something like this

var myDate = new GlideDateTime('<your date>');

var gdt = new GlideDateTime();
var time = gdt.toString().split(' ')[1];

var hour = time.split(':')[0];

hour = parseInt(hour);

if(hour > 15){
	myDate.addDaysUTC(3);
}

// now use myDate to set the value

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

use this

addDays() is not allowed in scoped app

use addDayUTC()

var gdt = new GlideDateTime();

gdt.addDaysUTC(3);

gs.info(gdt);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Dhez Vu
Tera Expert

Hi Ankur, 

thankyou for the reply. but my requirement was if the current system is > 3PM then the date field should be updated to 3days from now. 

Can you please help me with that 

the above script will do that

when you use new GlideDateTime() it uses GMT and since you are adding UTC days it works well

var gdt = new GlideDateTime();

gs.info('now time' + gdt);

gdt.addDaysUTC(3);

gs.info('after 3 days time' + gdt);

[0:00:00.067] Script completed in scope x_421457_testing: script


Script execution history and recovery available here


x_421457_testing: now time2022-01-25 05:30:28
x_421457_testing: after 3 days time2022-01-28 05:30:28

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Dhez Vu
Tera Expert

but I have to check the current system time is greater than 3 PM that condition is throwing an error in the GlideDateTime object. 

 

if (currentSystemTme > 15) //24hr clock  - this condition is not working as expected 

then

gdt.addDaysUTC(3);

 

Thankyou so much for the quick response. Appreciated