The CreatorCon Call for Content is officially open! Get started here.

Issue in adding the number of hours to the current date & time in uers's timezone

Mehak1
Giga Expert

Hi All,

I want to update the field 'Planned End Date' with 'current DateTime + 4 hrs'. For this, I have used the below code:

 

var date = gs.nowDateTime();

var end= new GlideDateTime(date);

var hours = 60*60*4; 

end.addSeconds(hours);

 

The issue is when I am using the above code then my  'Planned End Date' field get populated with 'current DateTime + 6 hrs'. It will automatically add 2 extra hours even though I am adding seconds according to 4 hrs.

My User's Timezone is in CET. I want the value according to user's timezone. 

Could anyone tell me how to fix this issue?

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Just try this

 

var gdt = new GlideDateTime();
var hours = 60*60*4;

gdt.addSeconds(hours);
gs.print(gdt);

-Anurag

View solution in original post

5 REPLIES 5

Michael_Martin
Tera Contributor

you can also use add with a time object for adding hours like below.

var gdt = new GlideDateTime();
var gtime = new GlideTime();

gtime.setValue("04:00:00");  // 4 hours
gdt.add(gtime);

gs.print(gdt);

// just make sure, that you never mix setDisplayValue/getDisplayValue with objects without them.
// best practice is to use the local time, as servicenow will automatically convert it when needed.