Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need to Get the day based on entered date & time

is_12
Tera Contributor

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,

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@is_12 

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

AnkurBawiskar_1-1761291386161.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

@is_12 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

25 REPLIES 25

Ankur Bawiskar
Tera Patron
Tera Patron

@is_12 

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! 🙏

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

@Ankur Bawiskar @M Iftikhar 

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.

    function addMonths(date, months) {
        alert('Hello7');
        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('day_3', next3);
    alert('Hello22'+next3);
    g_form.setValue('day_6', next6);
    alert('Hello24'+next6);
    g_form.setValue('day_12', next12);
    alert('Hello26'+next12);
only first alert I'm getting post that I'm getting the error

@is_12 

I gave another easy code below with output.

Did you check that?

AnkurBawiskar_2-1761291450066.png

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar 

yes I have tried but it is getting set to today's date only

 

is_12_0-1761291675883.png