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

Add the duration variable to date and time variable and populat the end date value in string variab.

Community Alums
Not applicable

Hi Team,

 

I have created variable's in catalogs

 

1 start date - Date and time type variable

2 required time - Duration type variable

3 End time -  String type.

 

I need to add the duration and date and time variables. And populate the end time in string 

 

Can you please help me requriment.

 

Thank you

Siva

2 ACCEPTED SOLUTIONS

Rupanjani
Giga Guru

Hi @Community Alums,

 

Check the below link, it may be of any help for you.

https://www.servicenow.com/community/now-platform-forum/add-duration-to-date-time/m-p/1153814

 

Try below script and check:

onChange Client Script (start date), make sure the duration is first selected and then the start date.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var gaChng = new GlideAjax('Test'); //script include name
    gaChng.addParam('sysparm_name', 'getEndDate'); //second parameter - function name in the SI
    gaChng.addParam('sysparm_date', newValue);
    gaChng.addParam('sysparm_duration', g_form.getValue('u_duration'));
    gaChng.getXML(getData2);

    function getData2(response) {
        var answer2 = response.responseXML.documentElement.getAttribute('answer');
        if (answer2) {
            g_form.setValue('end_date', answer2);
        }
    }
}

 

 

Script Include (client callable checkbox - true):

 

var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEndDate: function() {
        try {
            var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
            var dur = new GlideDuration(this.getParameter('sysparm_duration'));
            gdt.add(dur);
            return gdt.getValue();
        } catch (ex) {
            gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
        }
    },

    type: 'Test'
});

 

 

 


Mark the response correct and helpful if the answer assisted your question.

View solution in original post

Something like this should work. 

Add pass the fields to line 1 and 2

var gdt = new GlideDateTime('pass fiueld value');
var dur = new GlideDuration(<pass field value>);
//dur.setValue('1970-01-02 00:00:00');
gdt.add(dur);
gs.info(gdt.getDisplayValue());
-Anurag

View solution in original post

9 REPLIES 9

Yashsvi
Kilo Sage

Hi @Community Alums, 

please check below link:

https://www.servicenow.com/community/itsm-forum/add-days-to-date-time-field-and-show-on-service-portal/m-p/2680314

Thank you, please make helpful if you accept the solution.

Community Alums
Not applicable

Hi @Yashsvi ,

 

Thank you for quick response,

 

I am tried below script. But not worling

 

Script inculde

var UtilsAjax = Class.create();
UtilsAjax.prototype = {
    initialize: function() {
    },
endtime: function() {
         var selected_date = new GlideDate();
         selected_date.setDisplayValue(this.getParameter('sysparm_Date'));
      //   var current = new GlideDate();

      var radio_button = this.getParameter('sysparm_Duration');
        //var start_date = this.getParameter('sysparm_Date');
         var start_check = gs.dateDiff(selected_date,radio_button, true);

       


         return start_check;
    },
    type: 'UtilsAjax'
};
 
Client script
 
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var duraton = g_form.getValue('duration');
var start_date = g_form.getValue('start');
var ga = new GlideAjax('UtilsAjax');
        ga.addParam('sysparm_name', 'endtime');
        ga.addParam('sysparm_Date', start_date);
        ga.addParam('sysparm_Duration',duraton);
        ga.getXML(CheckDateValidation);
    }

function CheckDateValidation(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
   
}
 
Can you please check code
 
Thank you 
Challa reddy

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Community Alums 

 

https://www.servicenow.com/community/developer-forum/how-to-calculate-end-date-time-based-on-start-date-time-and/m-p/2708085

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Community Alums
Not applicable

Hi @Dr Atul G- LNG ,

 

Thank you for your response.

 

I tried below client script its getting null value

Script include:

var UtilsAjax = Class.create();
UtilsAjax.prototype = {
    initialize: function() {
    },
   
    endtime: function() {
        var ab = getParameter('sysparm_Date');
        var ac = getParameter('sysparm_Duration');
        var gdt = new GlideDateTime(ab);
        var duration = parseInt(this.getParameter('sysparm_Duration'), 10);
        //var duration = parseInt(this.getParameter('sysparm_Duration'), 10);
         gdt.addMinutes(duration);
       
       // return selected_date.getDisplayValue();
        return gdt;
    },
 
    type: 'UtilsAjax'
};
 
Client Script:
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   
    var duration = g_form.getValue('duration');
    var start_date = g_form.getValue('start');
   
    var ga = new GlideAjax('UtilsAjax');
    ga.addParam('sysparm_name', 'endtime');
    ga.addParam('sysparm_Date', start_date);
    ga.addParam('sysparm_Duration', duration);
     ga.getXML(callback);
     function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);

        g_form.setValue('end', answer);

    }
   

}
 

Can you please check my code .
thank you in advance
 
Sivananda