Grabbing month only from date variable

Evan Duran
Kilo Guru

I wanted to add a 'Date Submitted Month' variable field to a catalog and have it grab the MONTH from 'Date Submitted' field. How do I go about pulling the month and returning it as string such as screenshot below
find_real_file.png

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Evan Duran

you can use onChange client script on that Date Submitted variable and script like this

UI Type - ALL

Applies to Catalog Item - True

Script:  It will take care of the logged in user Date format may be yyyy-mm-DD or mm-DD-yyyy etc

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

	if(newValue == '')
		g_form.clearValue('variableName'); // give here the name of the variable where you wish to store the month name

	if(oldValue != newValue){
		var month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
		var dt1 = new Date(getDateFromFormat(newValue, g_user_date_format));
		var monthNumber = dt1.getMonth();
		var name = month[monthNumber];
		g_form.setValue('variableName', name); // give here the name of the variable where you wish to store the month name
	}
}

Sample Script I tried:

find_real_file.png

Regards
Ankur

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

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Evan Duran

you can use onChange client script on that Date Submitted variable and script like this

UI Type - ALL

Applies to Catalog Item - True

Script:  It will take care of the logged in user Date format may be yyyy-mm-DD or mm-DD-yyyy etc

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

	if(newValue == '')
		g_form.clearValue('variableName'); // give here the name of the variable where you wish to store the month name

	if(oldValue != newValue){
		var month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
		var dt1 = new Date(getDateFromFormat(newValue, g_user_date_format));
		var monthNumber = dt1.getMonth();
		var name = month[monthNumber];
		g_form.setValue('variableName', name); // give here the name of the variable where you wish to store the month name
	}
}

Sample Script I tried:

find_real_file.png

Regards
Ankur

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

Hello, 

I used your script to get Month Name. But arrays starts from 0 to n-1, so if I get monthNumber as 4, I am getting Month name as May.

Is this same happened to you to?

 

What is the best way to get it right month in such case?

 

Thank you.

 

@Kawshal29 

Can you post a new question and share all the details and tag me there as this is an old thread?

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