
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2023 05:46 PM
Ok so I have two variable date fields:
- start_date
- end_date
Once the start date is entered I want the end date to automatically be updated to 90 days after the start date.
So I have added an on change catalog client script as follows:
Type - onChange
Variable Name - start_date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var addDays = "90";
var StartDate = new Date(g_form.getValue('sta_start_date_approved'));
var addDaysToStartDate = "";
var getDateFromStartDate = StartDate.getDate();
var totalDays = parseInt(getDateFromStartDate) + parseInt(addDays);
StartDate.setDate(totalDays);
addDaysToStartDate = formatDate(StartDate, g_user_date_format);
g_form.setValue('sta_end_date', g_user_date_format);
}
This is sort of working, but what is happening is it is not using the correct date format.
What I noticed when I changed the var addDays = "90" to only = 3, then I could see what it was doing was ultimately changing the date format of the end_date field.
eg:
my start_date uses the format dd/MM/yyyyy as should the end_date, But
when it added the 3 days it would move the dd to be where the MM is and add 3 days to the MM instead.
This sounds very complicated sorry.
So this date: 01/12/2023 (meaning 1 Dec 2023) would change to be 12/04/2023 - so the 3 days when using the date format of dd/MM/yyyy was really updating the month thinking it was the days.
I hope that makes sense to someone who can help me?
I need the date format to stay the correct way in the end_date field and add the days to the correct part of the date.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2023 07:36 PM
Well, I am trying to post a reply containing the correct code, but stupid Community won't let me, supposedly the edits need to be approved by some moderator.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2023 08:01 PM
Solution provided by @-O-
function onChange (control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var addDays = 90;
// Calcualte how many milliseconds the desired number of days contain
var offsetMilliseconds = addDays * 24 * 60 * 60 * 1000;
// Get the value in field (make sure to cut off the time part - if the field is in fact a date/time vs. date)
var startDateString = g_form.getValue('sta_start_date_approved').substr(0, g_user_date_format.length);
// Have ServiceNow API compute Unix epoch milliseconds out of the date string
var startDateValue = getDateFromFormat(startDateString, g_user_date_format);
// Calculate hos mony milliseconds the end date contains
var endDateValue = startDateValue + offsetMilliseconds;
// Turn the Unix epoch milliseconds into a Date
var endDate = new Date(endDateValue);
// Set the end date value
g_form.setValue('sta_end_date', formatDate(endDate, g_user_date_format));
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2023 08:03 PM
So for whatever reason the solution provided by @-O- won't save as text / code here on the community, but it certainly works.
Here is a screenshot of the code for others in the future potentially

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2023 08:04 PM
Here is the solution provided by @Tai Vu
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var addDays = 90;
var StartDate = getDateFromFormat(g_form.getValue('sta_start_date_approved'), g_user_date_format);
StartDate = new Date(StartDate); //Add this line to your script
var addDaysToStartDate = "";
var getDateFromStartDate = StartDate.getDate();
var totalDays = parseInt(getDateFromStartDate) + addDays;
StartDate.setDate(totalDays);
addDaysToStartDate = formatDate(StartDate, g_user_date_format);
g_form.setValue('sta_end_date', addDaysToStartDate);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 02:52 PM
This has become easier to do now using a flow with no coding required.
Here is a link to the instructions: