- 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
something like this in client script will help you
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  // Parse start date as JS date object
  var startDate = new Date(newValue);
  // Get day of the week (e.g., "Sunday", "Monday")
  var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  var dayName = days[startDate.getUTCDay()];
  g_form.setValue('u_day_of_week', dayName); // Replace 'u_day_of_week' with the field you want
  // Calculate Next 3, 6, and 12 Months Dates
  function addMonths(date, months) {
    var d = new Date(date);
    var day = d.getDate();
    d.setMonth(d.getMonth() + months);
    // Adjust if date overflows (e.g., April 31)
    if (d.getDate() < day) {
      d.setDate(0);
    }
    return d;
  }
  var date3 = addMonths(startDate, 3);
  var date6 = addMonths(startDate, 6);
  var date12 = addMonths(startDate, 12);
  // Format as yyyy-MM-dd (ServiceNow Date field format)
  function formatSNDate(d) {
    var mm = String(d.getMonth() + 1).padStart(2, '0');
    var dd = String(d.getDate()).padStart(2, '0');
    var yyyy = d.getFullYear();
    return yyyy + '-' + mm + '-' + dd;
  }
  g_form.setValue('u_next_3_months', formatSNDate(date3));   // Replace with your catalog variable
  g_form.setValue('u_next_6_months', formatSNDate(date6));   // Replace with your catalog variable
  g_form.setValue('u_next_12_months', formatSNDate(date12)); // Replace with your catalog variable
}
💡 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 @Community Alums
I have tried to implement the based on the start date, need to get the next 3months & 6months & 12months date, but both of the code, which you guy's have given is getting the console javascript error on the portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago - last edited a week ago
I gave another easy code below with output.
Did you check that?
💡 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
