Need to pull a date from one field, add a year, and set it into another field

jacobpeterson
Mega Expert

Hello all,

I'm trying to pull the date from a date field and add a year to it and then place it in another date field.

current code:

var startDate = g_form.getValue('accessStartDate');

var oneYear = startDate;

oneYear.setFullYear(oneYear.getFullYear() + 1);

oneYear = oneYear.toISOString().slice(0,10);

g_form.setValue('accessEndDate', oneYear);

1 ACCEPTED SOLUTION

ahammoud
Tera Guru

Hello Jacob,


You need to do this in 2 separate scripts, but you have to watch out the client date format they set in user preferences.


1) A client script to get the Date


2) a Server-side GlideAjax script to manipulate the Date



The client script would look something like this, but first you must identify the Date format the user:


var format = g_user_date_format;


var tdate = new Date();


var tdateStr = formatDate(tdate, format);


var tdateNum = getDateFromFormat(tdateStr, g_user_date_format);


var startdate = g_form.getValue('accessStartDate');


      var startdateStr = getDateFromFormat(startdate, format);


// get position of year month and day based on user format


var yearIndex = format.search("yyyy");  


var mmIndex = format.search("MM");


var ddIndex = format.search("dd");


// get year, month and day of Start date


var yearStart = startdate.substr(yearIndex,4);


var monthStart = startdate.substr(mmIndex,2);


var dayStart = startdate.substr(ddIndex,2);


// create Start date in proper server format


var serverStartDate = yearStart + "-" + monthStart + "-" + dayStart;


//add 12 months from start date


var ajaxDate = new GlideAjax('AjaxDateUtil');


ajaxDate.addParam('sysparm_name', 'addTwelveMonths');


ajaxDate.addParam('sysparm_sdate', serverStartDate);


ajaxDate.getXML(updateReturnDate);




function updateReturnDate (response){


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


// once we get back answer it is in server Date format yyyy-mm-dd convert to user format


// and place it in end date


var getUserFormat = g_user_date_format;



var yearClient = answer.substr(0,4); //get year


var monthClient = answer.substr(5,2); //get month


var dayClient = answer.substr(8,2); //get day




//create End Date based on user format


var newEndDate = getUserFormat.replace("yyyy", yearClient).replace("MM", monthClient).replace("dd", dayClient);


//alert("newEndDate " + newEndDate);




g_form.setValue('accessEndDate', newEndDate);




}



Next you will need to create a Script Include we'll name is "AjaxDateUtil" that is Client Callable that would like this



var AjaxDateUtil = Class.create();


AjaxDateUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {


//to add 12 months from period of start date


addTwelveMonths: function () {



var startDate = this.getParameter('sysparm_sdate');



var glideStartDate = new GlideDateTime(startDate);


glideStartDate.addMonths(12);



//gs.log("glideStartDate after 6 motnhs " + glideStartDate);


var newDate = glideStartDate.getDate();



return newDate;


},






      type: 'AjaxDateUtil'


});




I recommend reading up more on GlideDateTime methods described by ServiceNow.


Ideally do all your Date/Time manipulation using Server-Side to avoid conflict of Date/time format and Timezones.


Hope this helps please let me know.


View solution in original post

2 REPLIES 2

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Jacob,



ServiceNow has a lot of helpful methods for manipulating dates and times, but they're mostly server side. I think the standard practice for something like this is to user GlideAjax to process adding the time in a server side script include then return the correct data/time back to the client. I would take a look at this post where someone already did a lot of the work for you:



Client Script Date/Time Functions


ahammoud
Tera Guru

Hello Jacob,


You need to do this in 2 separate scripts, but you have to watch out the client date format they set in user preferences.


1) A client script to get the Date


2) a Server-side GlideAjax script to manipulate the Date



The client script would look something like this, but first you must identify the Date format the user:


var format = g_user_date_format;


var tdate = new Date();


var tdateStr = formatDate(tdate, format);


var tdateNum = getDateFromFormat(tdateStr, g_user_date_format);


var startdate = g_form.getValue('accessStartDate');


      var startdateStr = getDateFromFormat(startdate, format);


// get position of year month and day based on user format


var yearIndex = format.search("yyyy");  


var mmIndex = format.search("MM");


var ddIndex = format.search("dd");


// get year, month and day of Start date


var yearStart = startdate.substr(yearIndex,4);


var monthStart = startdate.substr(mmIndex,2);


var dayStart = startdate.substr(ddIndex,2);


// create Start date in proper server format


var serverStartDate = yearStart + "-" + monthStart + "-" + dayStart;


//add 12 months from start date


var ajaxDate = new GlideAjax('AjaxDateUtil');


ajaxDate.addParam('sysparm_name', 'addTwelveMonths');


ajaxDate.addParam('sysparm_sdate', serverStartDate);


ajaxDate.getXML(updateReturnDate);




function updateReturnDate (response){


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


// once we get back answer it is in server Date format yyyy-mm-dd convert to user format


// and place it in end date


var getUserFormat = g_user_date_format;



var yearClient = answer.substr(0,4); //get year


var monthClient = answer.substr(5,2); //get month


var dayClient = answer.substr(8,2); //get day




//create End Date based on user format


var newEndDate = getUserFormat.replace("yyyy", yearClient).replace("MM", monthClient).replace("dd", dayClient);


//alert("newEndDate " + newEndDate);




g_form.setValue('accessEndDate', newEndDate);




}



Next you will need to create a Script Include we'll name is "AjaxDateUtil" that is Client Callable that would like this



var AjaxDateUtil = Class.create();


AjaxDateUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {


//to add 12 months from period of start date


addTwelveMonths: function () {



var startDate = this.getParameter('sysparm_sdate');



var glideStartDate = new GlideDateTime(startDate);


glideStartDate.addMonths(12);



//gs.log("glideStartDate after 6 motnhs " + glideStartDate);


var newDate = glideStartDate.getDate();



return newDate;


},






      type: 'AjaxDateUtil'


});




I recommend reading up more on GlideDateTime methods described by ServiceNow.


Ideally do all your Date/Time manipulation using Server-Side to avoid conflict of Date/time format and Timezones.


Hope this helps please let me know.