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.

Set Month Name from Date in Scoped Application

nealratner
Mega Expert

For reporting purposes, I need to know what Month an inspection has been performed.   This is a scoped application, so GlideDateTime doesn't work and neither does getMonth() as far as I can tell.   I have been able to pull the month number, but the closest I have come is "undefined" when I am trying to get the Month name.   I have a single client script onChange from the variable date field 'initial_inspection_date' and want to populate 'inspection_month' field with January or the correlating name.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

          return;

    }

  var inspectionDate = g_form.getValue('initial_inspection_date');

  var date = inspectionDate.split('-');

  var monthNumber = date[1];

  function getMonthNameFromMonthNumber(monthNumber)

  {

  var monthNames = ["January", "February", "March", "Arpil", "May", "June", "July", "August", "September", "October", "November", "December"];

  return monthNames[monthNumber];

  }

  alert(getMonthNameFromMonthNumber(monthNumber));     //results in alert with "undefined"

  //g_form.setValue('inspection_month',getMonthNameFromMonthNumber(monthNumber));       ---populates 'inspection_month' field with "undefined"

   

}

Any help is greatly appreciated!!!

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Have you tried converting your month number from a string to an integer?



var monthNumber = parseInt(date[1], 10);


View solution in original post

12 REPLIES 12

Chuck Tomasi
Tera Patron

Have you tried converting your month number from a string to an integer?



var monthNumber = parseInt(date[1], 10);


Chuck...



First of all...you rock.     That worked.   The only issue I have is that '04' is saying 'May' now...



Do I need to do something in the array or how to interpret since the array starts at zero?


Hi Neal,



Since your arrays are always going to be zero-based, just use:



...


return monthNames[monthNumber - 1];



Then your "04" will return "Arpil"...




Thanks,


-Brian


Thanks for the kind words Neal. To add to Brian's comment, which is very good. Another option (because there's always different ways to do this stuff.)



var monthNumber = parseInt(data[1], 10) - 1;



I kind of like Brian's a bit better because it gives you a chance to validate the value of monthNumber too.



var monthNumber   = parseInt(data[1], 10);



// Just in case something really went bad, let's avoid returning an array element -1


if (monthNumber > 0)


        --monthNumber;



return monthNames[monthNumber];