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-
Kilo Patron

 

new Date(g_form.getValue('sta_start_date_approved'));

 

is wrong.
When the Date constructor is called with a string as parameter, the only standard format that it understands is YYYY-MM-DDTHH:mm:ss.sssZ.
Of course users will select wildly different date formats for themselves.
The proper way to turn a date field's (ultimately string) value into a Date, is to use function getDateFromFormat(val, format):

 

var StartDate = getDateFromFormat(g_form.getValue('sta_start_date_approved'), g_user_date_format);

 

Just out of curiosity, why

 

var addDays = "90";

 

and than

 

parseInt(addDays)

 

why not simply

 

var addDays = 90;

 

?

@-O- 

Thanks for the reply, firstly the script that I put up was not mine, so I just updated the field names and ran with it, it appeared to work other than the format issue, so happy to change any part of it that makes more sense.

 

Dates are something I don't totally get that's for sure.

 

I've updated my script as follows:

 

 

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', g_user_date_format);
}

Now it isn't updating the end date field at all?

 

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

 

Hi @Moedeb 

Let's correct this line.

From

 

g_form.setValue('sta_end_date', g_user_date_format);

 

To

 

g_form.setValue('sta_end_date', addDaysToStartDate);

 

 

And you also need to initiate the Date object for the StartDate variable.

Your script will be look like.

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); //Update this line
}

 

 

Cheers,

Tai Vu