How to calculate End Date/Time based on Start Date/Time and duration?

jenny32
Tera Guru

Hi,

In my table I have Start and End fields of "Date/Time" type and "Duration" field of "Integer" type. Based on the duration and start fields, I want End field should get populated. I have written code for this, the value is getting displayed on the form but when I click Submit button the value of End field is not getting saved in database. It is showing empty value.

Here is the code:

Client Script onSubmit:

function onSubmit() {

    //Type appropriate comment here, and begin script below

      var sdt = g_form.getValue('u_start_date_time'); //Choose the field to add time from  

      var addtime = g_form.getValue('u_duration_in_minutes'); //The amount of time to add  

      var addtype = 'minute'; //The time type.   Can be second, minute, hour, day, week, month, year.  

var ajax = new GlideAjax('ClientDateTimeUtils');  

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

ajax.addParam('sysparm_fdt', sdt);  

ajax.addParam('sysparm_addtime', addtime);  

ajax.addParam('sysparm_addtype', addtype);  

ajax.getXML(doSomething);  

 

  function doSomething(response){  

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

      //You could then take the new Date/Time answer and set the value of another field.  

      g_form.setValue('u_end_date_time', answer);

g_form.setReadOnly('u_end_date_time', 'true');

       

      alert(answer);  

}

}

Script Include:

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

//Takes a date/time field and adds time to it.  
//params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - second, minute, hour, day, week, month, year), sysparm_addtime (amount of time to add based on the type).  
addDateTimeAmount: function(){  
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field  
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())  
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add  
var day = GlideDateTime(firstDT);  
 
if(addTYPE == 'second'){day.addSeconds(addTIME);}  
else if (addTYPE == 'minute'){day.addSeconds(addTIME*60);}  
else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}  
else if (addTYPE == 'day'){day.addDays(addTIME);}  
else if (addTYPE == 'week'){day.addWeeks(addTIME);}  
else if (addTYPE == 'month'){day.addMonths(addTIME);}  
else if (addTYPE == 'year'){day.addYears(addTIME);}  
else {day.addDays(addTIME);}  
 
//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;  
return day;  
}

      //type: 'ClientDateTimeUtils'
});

Can anyone help me in resolving this!!!!

Thanks,

Jenny.

1 ACCEPTED SOLUTION

jenny32
Tera Guru

In my client script, I changed the "Type" from onSubmit to onChange and it worked. I was able to store the End Date/Time field value in the database.



Thanks,


Jenny


View solution in original post

9 REPLIES 9

LA_
Tera Expert

Hello.

I have a similar requirement, but my variable type is date (not date time).

I need to automatically calculate the end date that is 1 year from my start date.

Can you please advise?

thanks.

You can do something like that on date field too.

Example

Client script:

function onSubmit() {
    var sdt = g_form.getValue('u_start_date_time'); //Choose the field to add time from  
    var addtime = 1; //The amount of time to add  
    var addtype = 'year'; //The time type.   Can be second, minute, hour, day, week, month, year.  
    var ajax = new GlideAjax('ClientDateTimeUtils');
    ajax.addParam('sysparm_name', 'addDateTimeAmount');
    ajax.addParam('sysparm_fdt', sdt);
    ajax.addParam('sysparm_addtime', addtime);
    ajax.addParam('sysparm_addtype', addtype);
    ajax.getXML(doSomething);

    function doSomething(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        //You could then take the new Date/Time answer and set the value of another field.  
        g_form.setValue('u_end_date_time', answer);
        alert(answer);
    }
}

Script Include

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    //Takes a date/time field and adds time to it.  
    //params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - second, minute, hour, day, week, month, year), sysparm_addtime (amount of time to add based on the type).  
    addDateTimeAmount: function() {
        var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field  
        var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())  
        var addTIME = this.getParameter('sysparm_addtime'); //How much time to add  
        var day = GlideDateTime(firstDT);

        if (addTYPE == 'second') {
            day.addSeconds(addTIME);
        } else if (addTYPE == 'minute') {
            day.addSeconds(addTIME * 60);
        } else if (addTYPE == 'hour') {
            day.addSeconds(addTIME * (60 * 60));
        } else if (addTYPE == 'day') {
            day.addDays(addTIME);
        } else if (addTYPE == 'week') {
            day.addWeeks(addTIME);
        } else if (addTYPE == 'month') {
            day.addMonths(addTIME);
        } else if (addTYPE == 'year') {
            day.addYears(addTIME);
        } else {
            day.addDays(addTIME);
        }

        //return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;  
        return day;
    }

    //type: 'ClientDateTimeUtils'
});

Thanks Mike for the help. I also saw another solution from @asifnoor which also worked. I just modified it to add 365 days from selected date. Thanks @asifnoor !

 

https://community.servicenow.com/community?id=community_article&sys_id=a26dac761b4e8010a59033f2cd4bcb12 

find_real_file.png

Glad to know that my article helped 🙂

Sirri
Tera Guru

Hi Jenny,

 

let suppose two hours is there then how we can give in this scenario.

 

this is for the one hour you have given else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}  right.

 

same requirement for slight change duration type is drop down then how we can write please provide source code.