Add an year to a current date in client script

Khanna Ji
Tera Guru

Hi Team,

I have taken the below script from the thread but it's not giving desired results 

client script to add 1 year to current date - Now Platform - Question - ServiceNow Community

@Ankur Bawiskar 

var todayDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
g_form.setValue('valid_to', todayDate.toISOString().slice(0,10));

It is adding the dates as 

But it should be like below

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

image is broken

Script should work fine.

I just tried with the debugger.

find_real_file.png

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Aman Kumar S
Kilo Patron

Why are you not using GlideAjax to get the date:

How to add one year to a date field using client script(on change) + script include

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Mohith Devatte
Tera Sage
Tera Sage

Hello,

Please use glide ajax for this as it involves server side coding .

Create a script include and call it using glide ajax and in SI use below script to return the result

Client Script:

var ga = new GlideAjax('your script include name');
    ga.addParam('sysparm_name', "calculateDate");
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            alert(answer);

g_form.setValue('valid_to',answer);
        }

Script include:

calculateDate: function(){

        
        var year = 1;
        var gdt = new GlideDateTime();
        gdt.addYearsUTC(year);
        return gdt.getDate();
    },


    });

Please accept the solution if it answers your question

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

image is broken

Script should work fine.

I just tried with the debugger.

find_real_file.png

Regards
Ankur

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

@Khanna Ji 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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