How to add hours to a Date/Time value?

Lakshmi Prabha
Giga Expert

Hi,

How to add hours to a Date/Time value.

I want to add 13 hours to the below Time format.

**4beginNextWeek===>2016-08-08 04:00:00

I have tried this Its din't work.

var beginNextWeek = new GlideDateTime(gs.beginningOfNextWeek());

  gs.log("**4beginNextWeek===>"+beginNextWeek);

  var splitDate = beginNextWeek.split("-"); // Split Catalog Date into year, month, day  

var termDate = new Date(splitDate[0],splitDate[1]-1,splitDate[2]); // Avoid timezone issues, but month starts at 0  

termDate.setHours(14); // 5:00 p.m. EST (2:00 p.m. PST by default)  

  gs.log("**4termDate===>"+termDate);

1 ACCEPTED SOLUTION

You might want to use getDisplayValue() to get in the user's time zone



var gdt = new GlideDateTime(gs.beginningOfNextWeek());


var hours = 60*60*13;


gdt.addSeconds(hours);


gs.print(gdt.getDisplayValue());


View solution in original post

6 REPLIES 6

Chuck Tomasi
Tera Patron

Hi Lakshmi,



Use gs.hoursAgo() with a negative value to go forward in time.



See the example in section 3.3.0 here



GlideSystem Date and Time Functions - ServiceNow Wiki


jake_mckenna
ServiceNow Employee
ServiceNow Employee

I would suggest the GlideDateTime functions to help with this. More importantly the addSeconds('46800') as a way to add the correct time value.




GlideDateTime - ServiceNow Wiki


Better idea Jake! Thanks.



Here's an example how that might be done.



var beginStr= '2016-08-08 04:00:00';


var beginNextWeek = new GlideDateTime(beginStr);


var hours = 60*60*13;


beginNextWeek.addSeconds(days);


gs.info('new time=' + beginNextWeek.getValue());


You might want to use getDisplayValue() to get in the user's time zone



var gdt = new GlideDateTime(gs.beginningOfNextWeek());


var hours = 60*60*13;


gdt.addSeconds(hours);


gs.print(gdt.getDisplayValue());