Auto-populate Expiration date based on the Start date and Duration of months selected

Nagashree5
Tera Contributor

Hi All,

 

Below are the fields on the form level.

Nagashree5_0-1697640605501.png

Based on the start date and duration of months selected (it is a dropdown of 1 to 12 numbers) - Expiration date should be auto populated.

suppose if the start date is selected as 10-18-2023 and duration of months is selected as 3 months, the Expiration date needs to be auto populated as 10-18-2023 + 90 days.

Can anyone guide me on this.

 

TIA.

15 REPLIES 15

Vanderlei
Mega Sage

Hi @Alka_Chaudhary,@Nagashree5 


I made all the necessary changes to the code and tested it on my PDI

Follow the code below, please disregard the last code I sent

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

    var date_number = getDateFromFormat(g_form.getValue("poa_start_date"), g_user_date_time_format)
    var my_date = new Date(date_number);
    alert(my_date.toLocaleDateString("en-GB"));// dd/mm/yyy , change to en-US to get mm/dd/yyyy
    var newDate = new Date(my_date.setMonth(my_date.getMonth() + 1 + parseInt(newValue)));;
    alert(newDate.toLocaleDateString("en-GB"))// dd/mm/yyy , change to en-US to get mm/dd/yyyy
}


If this code doesn't work, try this code

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

    var date_number = getDateFromFormat(g_form.getValue("poa_start_date"), g_user_date_time_format)
    var my_date = new Date(date_number);
    alert(my_date.toLocaleDateString("en-GB"));// dd/mm/yyy , change to en-US to get mm/dd/yyyy
    var newDate = new Date(my_date.setMonth(my_date.getMonth() + 1 + parseInt(newValue)));;
    alert(newDate.toLocaleDateString("en-GB"))// dd/mm/yyy , change to en-US to get mm/dd/yyyy
}

 

Vanderlei Catione Junior | LinkedIn

Senior ServicePortal Developer / TechLead at The Cloud People



References:

Date: Javascript
Client Side Dates in ServiceNow