- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 01:40 PM
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
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 01:38 AM
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:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 01:46 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 01:49 PM
hello
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 01:52 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 12:08 AM
hello
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');
}