Allow only last day of month to be selected in date variable

Info-Remy
Giga Contributor

I have a requirement for a variable to display month and year. However, there is no OOB method for this. I have two options, and i want to know how feasible this is and how do i do it. the options are;

- Block out all dates except the last day of the month in calender or

- Throw an error message if any other day but the last day of the month is selected.

 

Any help is appreciated.

 

Thanks 

1 ACCEPTED SOLUTION

Thanks for your response. When i try your code, it sets the date as below.find_real_file.png

What could be the issue here?

View solution in original post

6 REPLIES 6

Jan Cernocky
Tera Guru

Hi,

I would add another 2 options

a. Keep standard date format as soon as the person selects certain day, set the date to the last day of that month and show info message (more user friendly than error message and forcing user to do another selection)

b. Simply have a variable of the date but do not show it in the form and in the form rather have two select boxes with list of months and list of years. Upon submit calculate the date for the date variable and show that one in the RITM/SCTASK form.

I would personally go for b.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Remy,

I would create an onChange() script on the date field to change the date to the last day of the month.

That is, I'll let end-user pick the day, but will let ServiceNow change the date to the last day of the month.

 

Something like below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var curDate = getDateFromFormat(newValue, g_user_date_format);
    curDate = new Date(curDate);
	var lastDay = new Date(curDate.getFullYear(), curDate.getMonth() + 1, 0);
	lastDay = lastDay.getFullYear() + '-' + zeroPad((lastDay.getMonth()+1)) + '-' + zeroPad(lastDay.getDate());
	if (newValue != lastDay) {
		g_form.setValue('last_day_of_month', lastDay);
	}
	
	function zeroPad(d) {
		return ('00'+d).slice(-2);
	}
}

Execution results:

Step 1: Enter date

find_real_file.png

Step 2: Changed to last day of month

find_real_file.png

Thanks for your response. When i try your code, it sets the date as below.find_real_file.png

What could be the issue here?