How to configure a catalog client scripts for the duration of two dates on catalog item?

A Icen
Tera Expert

hi everyone, 

I have a catolog item in which the duration time difference is calculated and populated when start and end date are picked up. I need a catalog client scripts, I have attached the screen to give you an idea what I need exactly. 

Thanks 

1 ACCEPTED SOLUTION

Hi 

The previous code did not work. 

I have configured the code below, and it worked as I did want. Just keep in mind that use this code for both start date and end date on variable (not duration)

function onChange(control, oldValue, newValue, isLoading) {
    // Get the values of start_date and end_date fields


    // Check if both fromDate and toDate have valid values
    if (g_form.getValue('start_date') && g_form.getValue('end_date')) {
    var fromDate = g_form.getValue('start_date');
    var fromDateValues = fromDate.split('/');
   
    var toDate = g_form.getValue('end_date');
    var toDateValues = toDate.split('/');
        // Create Date objects for start_date and end_date
        var fromDateObj = new Date(fromDateValues[2], fromDateValues[1], fromDateValues[0]);
        var toDateObj = new Date(toDateValues[2], toDateValues[1], toDateValues[0]);

        // Calculate the difference in milliseconds
        var timeDifference = toDateObj.getTime() - fromDateObj.getTime();
       
        // Convert milliseconds to days
        var daysDifference = timeDifference / (1000 * 3600 * 24);
       
        // Round the result to get the total number of days
        var totalDays = Math.round(daysDifference);

        // Set the value of the 'duration' field
        g_form.setValue('duration', totalDays);
    }
}

View solution in original post

10 REPLIES 10

Hi @kkrushkov 
Thanks for your quick response. I have done what you have suggested in your message. But it did not work. I have also attached the screenshots. Where might I have made a mistake? 

Thanks again

@A Icen Change the variable name in the on change client script to be the end date or the start date, not the duration. 

Hi 

The previous code did not work. 

I have configured the code below, and it worked as I did want. Just keep in mind that use this code for both start date and end date on variable (not duration)

function onChange(control, oldValue, newValue, isLoading) {
    // Get the values of start_date and end_date fields


    // Check if both fromDate and toDate have valid values
    if (g_form.getValue('start_date') && g_form.getValue('end_date')) {
    var fromDate = g_form.getValue('start_date');
    var fromDateValues = fromDate.split('/');
   
    var toDate = g_form.getValue('end_date');
    var toDateValues = toDate.split('/');
        // Create Date objects for start_date and end_date
        var fromDateObj = new Date(fromDateValues[2], fromDateValues[1], fromDateValues[0]);
        var toDateObj = new Date(toDateValues[2], toDateValues[1], toDateValues[0]);

        // Calculate the difference in milliseconds
        var timeDifference = toDateObj.getTime() - fromDateObj.getTime();
       
        // Convert milliseconds to days
        var daysDifference = timeDifference / (1000 * 3600 * 24);
       
        // Round the result to get the total number of days
        var totalDays = Math.round(daysDifference);

        // Set the value of the 'duration' field
        g_form.setValue('duration', totalDays);
    }
}

Maddysunil
Kilo Sage

@A Icen 

 

var startDateObj = new Date(startDate);
    var endDateObj = new Date(endDate);

    // Calculate the duration difference in milliseconds
    var durationDiff = endDateObj - startDateObj;

    // Convert the duration difference to hours
    var durationHours = durationDiff / (1000 * 3600); // milliseconds to hours

    // Set the calculated duration value to the field
    g_form.setValue('duration', durationHours);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

Hi Maddysunil, 

Where should I write this? I need a "catalog client scripts" to calculate it. If you have any idea and can help me I will appreciate it. 

Thanks