Which field can use to count the days between start day and End dday

Anuj20
Tera Contributor

HI All

 

Please help to create a field in Contract Form, which can count the days Between start day to end day

 

for example start date is 09/10/2023 and end date is 08/10/2024

 

So that fill will update daily as 1 2 3 4 5 . . . . . 365.

2 REPLIES 2

Bert_c1
Kilo Patron

Hi,

 

you can create a new field of Type "Duration", and add logic calculate the difference between the two fields.

 

FieldTypes

 

and

duration calculation

 

good luck

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Anuj20 ,

 

Please create a new field (Name whatever you want) as "Total Duration" of type as String. Then u need to create 2 on Change script which should trigger on start date or end date field whenever they are changed.

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

var startDate = g_form.getValue('start_date'); // please update proper backend names of the field
var endDate = g_form.getValue('end_date'); // please update proper backend names of the field
    var fetchDuration = new GlideAjax('CalculateDate');
    fetchDuration.addParam('sysparm_name', 'totalDuration');
    fetchDuration.addParam('sysparm_start', startDate);
fetchDuration.addParam('sysparm_end', endDate);
    fetchDuration.getXMLAnswer(callback);

    function callback(answer) {
        if (answer){
g_form.setValue('total_duration',answer);
g_form.save();
}
}

 

 

Script Include:

 

 

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

    totalDuration: function() {

        var start = this.getParameter('sysparm_start');
        var end = this.getParameter('sysparm_end');

        var startDate = new GlideDateTime(start);
        var endDate = new GlideDateTime(end);
        var duration = GlideDateTime.subtract(endDate, startDate);
        var days = duration.getDayPart();

		return days;

    },
    type: 'CalculateDate'
});

 

 

Mark my answer helpful & accepted if it helps you resolve your issue.

 

Thanks,

Danish