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

Hi Ankur 

 

im chosing 10 & 12 as the start and end dates but the diff is coming as 61

@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

Hi Ankur, 

 

Thanks for the help im able to get the days diffrence.