- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 06:23 AM
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
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:29 AM
Hi,
image is broken
Script should work fine.
I just tried with the debugger.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 06:30 AM
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 🙂
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 06:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 07:29 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2022 10:30 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader