Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Muhammad Khan
Mega Sage

Hi,

 

Try the below script in background script.

var gd = new GlideDate(); 
gd.setValue('2021-12-21');

gs.print(gd.getByFormat("MMMM"));

Just change the month and verify you are getting accurate month name, then we can utilize it as per your requirement.

Mohith Devatte
Tera Sage
Tera Sage

hello @Evan Duran ,

you can try this in on change client script on "date submitted field"

you can do else if loops for 01 to 12 which indicates jan to dec

var spt = newValue.split('-');
if(spt[0]=='01')
{
g_form.setValue('your_month_field_name','January');
}
else if(spT[1]=='02')
{
g_form.setValue('your_month_field_name','February');
}
......
else if(spT[11]=="12")
{
g_form.setValue('your_month_field_name','December');
}

Hope this helps 

PLEASE MARK MY ANSWER CORRECT IF THIS HELPS YOU

If date entered is 2022-06-02

Will it read date as June or February? Because month is middle number '06' June, so how will it know to read middle number?

hello @Evan Duran you need to write else if loops like below so that it reads the month 

var spt = newValue.split('-');
if(spt[0]=='01')
{
g_form.setValue('your_month_field_name','January');
}
else if(spt[1]=='02')
{
g_form.setValue('your_month_field_name','February');
}
else if(spt[2]=='03')
{
g_form.setValue('your_month_field_name','March');
}
else if(spt[3]=='04')
{
g_form.setValue('your_month_field_name','April');
}
else if(spt[4]=='05')
{
g_form.setValue('your_month_field_name','May');
}
else if(spt[5]=='06')
{
g_form.setValue('your_month_field_name','June');
}
else if(spt[6]=='07')
{
g_form.setValue('your_month_field_name','July');
}
else if(spt[7]=='08')
{
g_form.setValue('your_month_field_name','August');
}
else if(spt[8]=='09')
{
g_form.setValue('your_month_field_name','September');
}
else if(spt[9]=='10')
{
g_form.setValue('your_month_field_name','October');
}
else if(spt[10]=='11')
{
g_form.setValue('your_month_field_name','November');
}
else if(spt[11]=="12")
{
g_form.setValue('your_month_field_name','December');
}