- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
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,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago - last edited a week ago
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
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
@Ankur Bawiskar But instead I need 3months or 6months date from today
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
I just now updated the script, are you using the correct one?
share your client script and is it marked as UI Type - ALL
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
@is_12 , Add below code in your on change script on start date field.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) return;
var startDate = new Date(newValue);
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dayName = days[startDate.getDay()];
function addMonths(date, months) {
var d = new Date(date);
d.setMonth(d.getMonth() + months);
return (
d.getFullYear() +
'-' +
String(d.getMonth() + 1).padStart(2, '0') +
'-' +
String(d.getDate()).padStart(2, '0')
);
}
var next3 = addMonths(startDate, 3);
var next6 = addMonths(startDate, 6);
var next12 = addMonths(startDate, 12);
g_form.setValue('u_day_name', dayName);
g_form.setValue('u_3_months_date', next3);
g_form.setValue('u_6_months_date', next6);
g_form.setValue('u_12_months_date', next12);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
Hi @Ankur Bawiskar & @Community Alums
Could you please refactor the code to support all the different date formats that ServiceNow provides as each user could change their date / time formats and if its not the OOB yyyy-MM-dd format then both the scripts will break as new Date() expects a specific format
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
Hi @Simon Christens , Try this code this should work for all regions and time formats.
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gdt = new GlideDateTime();
gdt.setDisplayValue(newValue);
var dayOfWeek = getDayName(gdt);
var date3 = new GlideDateTime(gdt);
date3.addMonthsLocalTime(3);
var date6 = new GlideDateTime(gdt);
date6.addMonthsLocalTime(6);
var date12 = new GlideDateTime(gdt);
date12.addMonthsLocalTime(12);
g_form.setValue('day_of_week', dayOfWeek);
g_form.setValue('next_3_months', date3.getDisplayValue());
g_form.setValue('next_6_months', date6.getDisplayValue());
g_form.setValue('next_12_months', date12.getDisplayValue());
}
function getDayName(gdt) {
var jsDate = new Date(gdt.getNumericValue());
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return days[jsDate.getDay()];
}
If this answer helps you in any way make sure to Mark this as accepted solution and give a thumbs up this will also benefit others as well.
Regards.
Saurabh V.
