Add 5 days to current date/time gs.nowDateTime()

etabalon
Mega Expert

I have a simple requirement and I thought this would be simple task but getting frustrated with the behavior.

If I returned gs.nowDateTime() from the script includes, the output was "01/03/2018 11:22:34"

If I added 5 days to the gs.nowDateTime(), it returns "2018-01-08", the computation was correct but the format I wanted is

"01/08/2018 00:00:00'

I have an onLoad script to populate a date/time variable adding 5 days from the current date/time.

onLoad script

function onLoad() {

var ajax = new GlideAjax('MyDateTimeAjax');

ajax.addParam('sysparm_name','nowDateTime');

ajax.getXML(checkdate);

function checkdate(response){

var currentDate = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('date_due', currentDate);

}

}

Script Includes

var MyDateTimeAjax = Class.create();

MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

nowDateTime: function () {

var sd = new GlideDateTime(gs.nowDateTime());

sd.addDaysLocalTime(5);

var startPlus5 = sd.getLocalDate();

return startPlus5;

//return gs.nowDateTime();

},

type: 'MyDateTimeAjax'

});

Any help would be appreciated.

Thanks

Enrique

2 REPLIES 2

sachin_namjoshi
Kilo Patron
Kilo Patron

Please check below thread which has code to add days in required format to current date.



Client Script Date/Time Functions



Regards,


Sachin


I found/read that thread and I noticed #8 about limitations...



addDateTimeAmount



With this ajax function you can add more time to a glide date/time field. You can add seconds, minutes, hours, days, weeks, months, and years. If you want to take time away, use a negative number in the addtime variable.



Limitation:



One limitation that I have found so far is in the return date/time format. The added-to-time new date/time is returned in the standard internal date/time format. I am trying to figure out how to return it based on the user defined date/time format. Still a work in progress.