Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need to Get the day based on entered date & time

is_12
Tera Contributor

Hi Community,

 

On the Catalog form I have field start & end date.

 

Based on the start date need to get the day

 And also based on the start date, need to get the next 3months & 6months & 12months date

 

Can anyone help me with this

 

Thanks,

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@is_12 

try this and it will take care of any date/time format

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

    // Parse string to milliseconds
    var dateMS = getDateFromFormat(newValue, g_user_date_time_format);
    var newDT = new Date();
    newDT.setTime(dateMS);

    // Example: Add 3 months
    newDT.setMonth(newDT.getMonth() + 3);

    // If you want 6 or 12 months, just change the +3:
    // newDT.setMonth(newDT.getMonth() + 6); // For 6 months
    // newDT.setMonth(newDT.getMonth() + 12); // For 12 months

    var val = formatDate(newDT, g_user_date_time_format);
    alert(val);
}

Output:I added 3 months, enhance further

AnkurBawiskar_1-1761291386161.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

@is_12 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

25 REPLIES 25

@is_12 

somehow script include is not getting called.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar, after leap year calculation, I'm using in schedule entity.

Is there any way to check in the leap year in condition.

is_12_0-1762758590682.png

based on the starting date entry

 

@Ankur Bawiskar, Thanks for your solution,

 

There is a small change instead of start date + 3months, based on selected "start date" we have day of month that is nothing but date based on that it should be +3 or +6

Below is the reference

is_12_0-1761308655015.png

 

Tried with this below script 

Client script

var dateMS = getDateFromFormat(newValue, g_user_date_time_format);
    var newDT = new Date();
    newDT.setTime(dateMS);
    //alert("8"+dateMS);
    // Example: Add 3 months



    var ga = new GlideAjax('BT_CSR_SSR_CatalogUtils');
    ga.addParam('sysparm_name', 'calDate');
    ga.addParam('sysparm_date', g_form.getValue('start_date'));
    ga.addParam('sysparm_dayofmonth',g_form.getValue('run_dayofmonth'));
    ga.getXML(check);

    function check(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert("22"+answer);
        var ansParsed = JSON.parse(answer);
         var gg = g_form.setValue('day_1', ansParsed);


    }

    var monthly = g_form.getValue('select_month');
    if (monthly == 'Every Month') {
        alert("12");
        newDT.setMonth(newDT.getMonth() + 1);
        var val = formatDate(newDT, g_user_date_time_format);
        g_form.setValue('day_1', val);
        alert("Monthly" + val);

}

script include

    calDate: function() {
        var sdt = new GlideDateTime(this.getParameter('sysparm_date'));
        sdt.addDaysLocalTime(this.getParameter('sysparm_dayofmonth'));
        return sdt.getDate();
    },
but getting javascript error
 
Thanks,

@is_12 

didn't get the requirement.

you need to add the value 21 (value in earlier variable) to the date entered by user?

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

@Ankur Bawiskar 

Let me explain you
Let say user has selected the start date as today(25th) & day of the month as 28(28 is nothing but date) & select month as Every month
then day1 should be from day of month ie 28th +1 rather than start date+1
similarly let say select month is quatarely then it should be 28th +3

 

Thanks