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

Gaurav Bajaj
Kilo Sage

Hi,

Try below script, this should work for you.

var end= new GlideDateTime();
var hours = 60*60*4; 
end.addSeconds(hours);

current.opened_at=end.getDisplayValue();

Please mark it correct/helpful based on the response.

Thanks
Gaurav

Mehak1
Giga Expert

Hi Gaurav,

I have implemented the changes as you have mentioned but still I am facing the same issue. Below are the results:

Planned Start Date: 2018-04-20 13:08

Planned End Date: 2018-04-20 19:08 

 

Thanks & Regards, 

Mehak Arora

Hi,

I did it on my change_request fields i.e. start and end time.

Based on the value of start time, I have updated the end time which is what I think you are looking for.

Please try below script and see the action.

var gr = new GlideRecord("change_request");
gr.get('7a25a8750f5517401536715ce1050ea7'); //get the record
var startDate=gr.start_date; //get the start time
var gdt = new GlideDateTime(startDate);
gdt.addSeconds(14400);  // no of seconds in 4 hours
gr.end_date=gdt.getDisplayValue();
gr.update();

Please mark it correct/helpful based on the response.

Thanks
Gaurav

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