Add 2 hours to Start date and End date

Lucky1
Tera Guru

Hi all,

 

From a change request, I am printing Start date and End date in scripts background.

I want to add 2 hours to the Start date and End date. Using below code I can add it to either start date or End date.

But how can I add it to both Start Date and End date at once using one GlideDateTime method?

var sd = current.start_time.getDisplayValue(); //getting start date value of CR

var ed = current.end_time.getDisplayValue(); //getting end date value of CR

 

var gdt = new GlideDateTime(sd);

var hours = 60*60*2;

gdt.addSeconds(hours);

 

 

So, please help

 

 

Regards,

Lucky

5 REPLIES 5

Sagar Pagar
Tera Patron

Hi @Lucky1,

 

Try this updated scripts,

var sd = current.start_time.getDisplayValue(); //getting start date value of CR
var ed = current.end_time.getDisplayValue(); //getting end date value of CR

 
var gdt = new GlideDateTime(sd);
var hours = 60*60*2;
gdt.addSeconds(hours);

current.start_time = gdt;

var gdt1 = new GlideDateTime(ed);
var hours = 60*60*2;
gdt1.addSeconds(hours);

current.end_time = gdt1;

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi @Lucky1,

 

Try this 

var current = new GlideRecord("change_request");
current.get("b0dbda5347c12200e0ef563dbb9a718f");

var start_date = new GlideDateTime(current.start_date);
var end_date = new GlideDateTime(current.end_date);
var hours = 2;
/************ Before adding 2 hours ***********/ 
gs.print('*********** Before adding 2 hours **********');
gs.print("start_date : "+start_date);
gs.print("end_date : "+end_date);

/*********** After adding 2 hours *****************/
start_date.addSeconds(hours*60*60);
end_date.addSeconds(hours*60*60);
gs.print('*********** after adding 2 hours **********');
gs.print("start_date : "+start_date);
gs.print("end_date : "+end_date);

 

Output:

Mohanraj_0-1689591116690.png

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

you need to do this separately as both the information are different

 

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

Oh, Ok Ankur.

 

Thank you