views:

122

answers:

5

i need to convert Monthname to integer of that month (and want to avoid a big switch statement). any ideas?

+3  A: 

You can create an array(name/vale pairs) in your code since it's only 12 months and write a function to do it.

var months = {August: 8};
Achilles
@Achilles - that not much different than a big switch statement but definately a backup idea
ooo
Not a big switch statement, Pointy has the same idea. it's only two lines of code for you.
Achilles
+8  A: 

Just create a date in that month, parse it, and use getMonth() like this

function convertMonthNameToNumber(monthName) {
    var myDate = new Date(monthName + " 1, 2000");
    var monthDigit = myDate.getMonth();
    return isNaN(monthDigit) ? 0 : (monthDigit + 1);
}

alert(convertMonthNameToNumber("August"));     //returns 8
alert(convertMonthNameToNumber("Augustsss"));  //returns 0 (or whatever you change the default too)
alert(convertMonthNameToNumber("Aug"));        //returns 8 - Bonus!
alert(convertMonthNameToNumber("AuGust"));     //returns 8 - Casing is irrelevant!
Chad
That's a good idea!
Pointy
I like this. For completeness, what will happen if I pass an invalid month (like "Äugust")?
Pekka
Worth mentioning that that *Date.parse()* returns a number representing the date, not an actual date object. Also *getMonth()* will return *7*, not *8* because months are zero-based.
Andy E
You beat me to it! But actually, this is wrong anyways :). `Date.parse` returns a number, which doesn't have the getMonth property. Try `var myDate = new Date(Date.parse(monthName + " 1, 2000"))`
CD Sanchez
@Daniel, I was already fixing it.
Chad
@Pekka, rewritten, to return 0 if the month is invalid
Chad
@Andy E, nice catches. I initially wrote it from memory, hadn't written tests yet. Should be good to go now though.
Chad
Personally, I would have have just returned `NaN` instead of 0. Seems to make more sense to me. Since it converts a month to name to a name, returning `NaN` would make it clear that it was not able to convert it. Thought that's just me being picky, and not by any means correct.
CD Sanchez
@Daniel, depends on how you are using it I suppose, but yes, `0` was a bad choice on my part, should probably be `NaN` or `null`, it was more just a placeholder to show that you need to check for `NaN` and can return whatever you want in that case.
Chad
+3  A: 
var monthtbl = { 'January': 1, 'February': 2, /* ... */, 'August', 8, /* ... */, 'December': 12 };
// ...
var monthNumber = monthtbl[monthName];

edit but do it the way @Chad suggests :-)

If you wanted to make it insensitive to alphabetic case, you'd create the object ("monthtbl") all lower-case and then use

var monthNumber = monthtbl[monthName.toLowerCase()];
Pointy
+1 That's the approach!
Achilles
This is solid. Out of interest: how would I make this case insensitive? In case I need to parse a user input?
Pekka
FWIW, this will obviously be faster if you're doing this many times.
Eric Wendelin
generate the array in uppercase, and uppercase the user input before checking the array.
Fosco
@Pointy, as mentioned, this has the downside of casing, and what if the month name were just **Aug** ?
Chad
@Fosco of course. D'oh.
Pekka
+2  A: 

Another option just to throw out there, you could use an array and $.inArry(), like this:

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function getMonth(name) {
  return $.inArray(name, months) +1;
}

Though judging by your previous questions, pulling the date directly from the jquery UI datepicker object may be much easier.

Nick Craver
Yikes! No need for jQuery here.
palswim
It could be replaced with Array.indexOf, but that isn't cross browser friendly.
CD Sanchez
@palswim it seems that "ooo" is already using jQuery because many of his recent questions are thusly tagged.
Pointy
@palswim - I agree in general, but I've answered some of the OPs previous questions so have a bit more information about his setup :)
Nick Craver
All right, then. Carry on!
palswim
A: 
function monthToNumber(month) {
    return new Date(Date.parse("1 "+month)).getMonth()+1;
}
CD Sanchez