Calculate days diffrence from 2 date fields in catalog ite

abjaffrey
Giga Guru

Hi, 

 

I have 2 date variables, need to calculate the diffrence between 2 dates and if its less than 30 days need to dispay a help text, Any suggestion how to achieve this.

 

Thanks

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@abjaffrey 

you can use onChange client script on both the date variable and determine the days and then show help text

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

	// hide help text or field message

    var startDate = new Date(g_form.getValue('variable1'));
    var endDate = new Date(g_form.getValue('variable2'));

    // Calculate the difference in time (milliseconds)
    var timeDifference = endDate - startDate;

    // Convert the time difference to days
    var dayDifference = timeDifference / (1000 * 3600 * 24);

    if (dayDifference < 30) {
        // show help text or field message
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@abjaffrey 

seems issue because of date format

with which date format did you try?

I tried with YYYY-MM-DD and it gave me correct output as 9 days for 1st Jan and 10th Jan

 

AnkurBawiskar_0-1736508145743.png

 

try this updated script

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

	// hide help text or field message

    var startDate = new Date(getDateFromFormat(g_form.getValue('variable1'), g_user_date_format));
    var endDate = new Date(getDateFromFormat(g_form.getValue('variable2'), g_user_date_format));

    // Calculate the difference in time (milliseconds)
    var timeDifference = endDate - startDate;

    // Convert the time difference to days
    var dayDifference = timeDifference / (1000 * 3600 * 24);

    if (dayDifference < 30) {
        // show help text or field message
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Gangadhar Ravi
Giga Sage
Giga Sage

Hi @abjaffrey you can try client script like below.

var one_day=1000*60*60*24;
var start = g_form.getValue('request_datetime').split(' ')[0];
var end = g_form.getValue('launch_date');
var date1_ms = new Date(start);
var date2_ms = new Date(end);
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
alert("Difference in days");
alert(Math.round(difference_ms/one_day));     

 Please mark my answer correct and helpful if this works for you.

hi,

 

i chose 10/01/25 & 12/01/25 as dates, the diffrence is coming as 61

Ankur Bawiskar
Tera Patron
Tera Patron

@abjaffrey 

should be an easy task

this link has solution, please enhance

Calculate Number of Days between two dates(Start and End Date) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@abjaffrey 

you can use onChange client script on both the date variable and determine the days and then show help text

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

	// hide help text or field message

    var startDate = new Date(g_form.getValue('variable1'));
    var endDate = new Date(g_form.getValue('variable2'));

    // Calculate the difference in time (milliseconds)
    var timeDifference = endDate - startDate;

    // Convert the time difference to days
    var dayDifference = timeDifference / (1000 * 3600 * 24);

    if (dayDifference < 30) {
        // show help text or field message
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader