1

I have a function based on a lot of if/else statements. The function works there is no problem with that but I wonder is there a shorter way to rewrite this?

month = get_month(field.value); //this is inside another function

function get_month(m){ if(m==='January'){return'01';} else if(m==='Febuary'){return'02';} else if(m==='March'){return'03';} else if(m==='April'){return'04';} else if(m==='May'){return'05';} else if(m==='June'){return'06';} else if(m==='July'){return'07';} else if(m==='August'){return'08';} else if(m==='September'){return'09';} else if(m==='October'){return'10';} else if(m==='November'){return'11';} else if(m==='December'){return'12';}}

I have seen and used this before but I don't know if would work on this scale:

var x = y !== undefined ? y : 1;
maček
  • 76,434
  • 37
  • 167
  • 198
Paul Ledger
  • 1,125
  • 4
  • 21
  • 46

8 Answers8

4

An object would fit this case rather well.

function getMonth(m) {
  var months = {
    January: "01",
    February: "02",
    March: "03",
    April: "04",
    May: "05",
    June: "06",
    July: "07",
    August: "08",
    September: "09",
    October: "10",
    November: "11",
    December: "12"
  }
  return months[m];
}

You can see this in action on this jsFiddle demo.

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
2

You could do something like

var monthNumbers = {
  "January": "01",
  "February": "02",
  //...
  "December": "12",
};

var getMonth = function(m) {
  return monthNumbers[m];
};

Better yet, if you're dealing with dates/times in JavaScript, I highly recommend moment.js

maček
  • 76,434
  • 37
  • 167
  • 198
1

Use key array:

var month = {
  January : "01",
  Febuary : "02",
  March : "03",
  April : "04",
  May : "05",
  June : "06",
  July : "07",
  August : "08",
  September : "09",
  October : "10",
  November : "11",
  December : "12"
}

function get_month(m){
  return month[m];
}
Mihail
  • 1,469
  • 11
  • 13
1

How about:

function getMonth(mon){
   var monthNum = new Date(mon +" 1, 2012").getMonth()+1;
   return ("0" + monthNum).slice(-2);
}

Inspired from this SO post.

Fiddle: http://jsfiddle.net/WFHWu/1/

Community
  • 1
  • 1
Jonah
  • 15,806
  • 22
  • 87
  • 161
  • Did you test this? Date.parse will return a number and not a Date object. You also can't use slice on numbers. – basilikum Nov 21 '13 at 23:17
  • Thanks. I tested the orginal, then made a change without testing (dumb). Edited and fixed now. http://jsfiddle.net/WFHWu/1/ – Jonah Nov 21 '13 at 23:29
0

I would use a lookup here (or, an inverse-lookup as the case may be).

The ternary (?:) is not an appropriate direct replacement due the extreme amount of nesting it would require.

// The lookup, uses a dummy object (that will never match the indexOf)
// such that January is at index 1, etc.
var months = [{}, 'January', .., 'December'];

// This just returns a number, and expects the caller to format the number
// for display as appropriate. Returns 0 for an invalid month name.
function get_month(m) {
  var monthNumber = months.indexOf(m); // -1 if not found
  return monthNumber > 0 ? monthNumber : 0;
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
0

For your specific task, you could also use the in-build Date object:

function get_month(m) {
    return new Date(m + " 1").getMonth() + 1;
}

However, this will return "1" and not "01", so you would have to write a function that adds the first zero when needed.

basilikum
  • 10,378
  • 5
  • 45
  • 58
-1

Use switch:

var ret;
switch (m) {
    case 'January':
        ret = '01';
        break;
    case 'Febuary':
        ret = '02';
        break;

    ...

}

Documentation.

Enam
  • 1,268
  • 1
  • 9
  • 16
-1

I'd recommend a switch statement instead. Your other example is a ternary operator and would be even messier.

NaNpx
  • 530
  • 3
  • 7