Updating a date automatically from another date

Moedeb
Tera Guru

Ok so I have two variable date fields:

  1. start_date
  2. 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.

 

 

1 ACCEPTED SOLUTION

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.

View solution in original post

13 REPLIES 13

@-O- & @Tai Vu 

My script now looks like this:

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);

    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);

}

 

When I update the start date field nothing is being updated in the end date field still.

 

I checked the dev tools on the page when I updated the start date field and the error that comes back now is:

Moedeb_0-1700710459829.png

 

 

 

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.

Hey @Moedeb 

Put this line to your script after getDateFromFormat, should do the trick.

StartDate = new Date(StartDate); //Add this line to your script

 

Refer to my comment above, it contains the adjusted code.

 

Cheers,

Tai Vu

I suppose I was "a bit" misleading there.

The function I mentioned does not in fact return a Date, but a Unix epoch milliseconds number.

So the code should look something like this:

 

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));
}

 

Moedeb
Tera Guru

So I have been lucky enough to receive two working scripts both two very helpful community members @-O-  and @Tai Vu . Thank you to you both.

 

I will attempt to post both working scripts for anyone in the future, however I've already tried once and the post didn't save/was deleted/something else, either way it isn't on here.

 

Would love to be able to select 2 solutions to give credit to you both, but unfortunately that is not a valid option.