- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 03:43 AM
I want to set Default value of "Expiry Date " field is +2 years from current date in DD-MM-YYYY format.
I have given Type of field as DATE for "Expiry Date " and written the below OnLoad() Client Script -
function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.isNewRecord()){
var todayDate = new Date(new Date().setFullYear(new Date().getFullYear() + 2)); // Now
g_form.setValue('expiry_date', todayDate.toISOString().slice(0,10));
}
}
// Output : Invalid Date while submitting new request.
Then I got to know "Date" type ideally took YYYY-MM-DD format ;So it is causing error as an Invalid Date.
But I want to set it by default in DD-MM-YYYY format as this is our company's default format .
How I can fix this issue? Please guide .
Regards,
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 08:43 PM
Hi,
the date format would be shown as per logged in user's date format
You need not worry on that.
Let the system set the date in system format yyyy-mm-DD and system will take care of other formats
you can use default value in that date field
javascript: var dt; var gdt = new GlideDateTime(); gdt.addYearsLocalTime(2); dt = gdt.getLocalDate(); dt;
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
06-13-2022 08:43 PM
Hi,
the date format would be shown as per logged in user's date format
You need not worry on that.
Let the system set the date in system format yyyy-mm-DD and system will take care of other formats
you can use default value in that date field
javascript: var dt; var gdt = new GlideDateTime(); gdt.addYearsLocalTime(2); dt = gdt.getLocalDate(); dt;
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
06-13-2022 11:42 PM
Hi Ankur,
Thanks for your suggestion. Yes it's working for me now.
Regards,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 09:43 PM
Hi Abhradipa,
Ankur's reply on setting default value should work but in case it's required to use client script, following client script will set the default date that is 2 years from the current date in field "expiry_date".
function onLoad() {
try {
var today = new Date();
today.setFullYear(today.getFullYear() + 2);
var dateFormat = g_user_date_format; // get end user's date format
dateFormat = dateFormat.replace('yyyy', today.getFullYear());
dateFormat = dateFormat.replace('MM', padZero(today.getMonth()+1));
dateFormat = dateFormat.replace('dd', padZero(today.getDate()));
g_form.setValue('expiry_date', dateFormat);
} catch (e) {
alert(e.message);
}
function padZero(str) {
var s = ('00' + str).slice(-2);
return s;
}
}
Execution output:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 11:44 PM
Hi Hitoshi,
I had tried with the default value separately as well as with the Client Script. Both approaches are working for me.
Thanks for your suggestion.
Regards,