Can not get a correct month name

Hi,

We have a web form which connects a Planet Press design template. On the web form, there is a dropdown button for a date to be entered, this button is connecting a calendar app. The calendar automatically popup the current data with the format like 2019-05-14. In the Java script, I got the exact value of this dropdown, 2019-05-14, than I was trying to use the split function to get the year, month and day: var splitDate = dateElement.split(‘-’) .

The year, month and day are all working fine to me. However, when I was going to get the month name with this piece of code, I got “undefined” error:

if (month === ‘05’) { monthName = ‘May’}

Stuck on this issue for days. Does any body have any clue?

Thanks,

John

I see a few potentitial issues here.
First, I assume you are assigning the results of the split to variables, i.e.:

var splitDate = dateElement.split(’-’);
myYear = splitDate[0];
myMonth = splitDate[1];
myDay = splitDate[2];

Then, note that the Month in JavaScript is 0-based, so January is 0 and May is 4.

Finally, when you use the === comparison, you are looking for an exact match in value AND type, which will not always work due to JavaScript’s automatic conversions.

You could simply do something like:

if (myMonth == 4) { var monthName = "May"}

Thanks Phil.

Here is my code:

var splitDate = dateElement.split(‘-’)
var year = splitDate[0]
var month = splitDate[1]
var day = splitDate[2]
var monthName = ‘’
if (month = 4)
{ monthName = ‘May’}

so far, worked fine, month == 4 doesn’t work for me. I got the month name, May.
Then, I replace the if (month = 4)
{ monthName = ‘May’}

with:
if (month = 0)
{ monthName = ‘Jan’}
else if (month = 1)
{ monthName = ‘Feb’}
else if (month = 2)
{ monthName = ‘Mar’}
else if (month = 3)
{ monthName = ‘Apr’}
else if (month = 4)
{ monthName = ‘May’}
else if (month = 5)
{ monthName = ‘Jun’}
else if (month = 6)
{ monthName = ‘Jul’}
else if (month = 7)
{ monthName = ‘Aug’}
else if (month = 8)
{ monthName = ‘Sep’}
else if (month = 9)
{ monthName = ‘Oct’}
else if (month = 10)
{ monthName = ‘Nov’}
else if (month = 11)
{ monthName = ‘Dec’}

I got the month name, Feb instead of May.

John

There are a couple of much easier ways to do this.
First one:

// var dateElement="2019-05-14";
var splitDate = dateElement.split("-");
var year = splitDate[0];
var month = splitDate[1];
var day = splitDate[2];
var monthnames=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
results.html(monthnames[month-1]);

Second one (preferred, and much simpler):

//var dateElement="2019-06-14";
var thisDate = new Date(dateElement);
var monthnames=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
results.html(monthnames[thisDate.getMonth()]);

Also note that in your previous example (with the multiple ifs), you were using a single “=” sign, which in JavaScript is not a comparison but an assignment operator.

To illustrate, try the following code

var myVar=4;
if(myVar=5){
	logger.info("This is always true because value 5 is assigned to myvar, instead of comparing the values");
}
logger.info("myVar now contains the value "+myVar);

Thanks a lot, Phil, for the help. Have a nice day !