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
2010-08-25 18:38:51
@Achilles - that not much different than a big switch statement but definately a backup idea
ooo
2010-08-25 18:39:38
Not a big switch statement, Pointy has the same idea. it's only two lines of code for you.
Achilles
2010-08-25 18:40:43
+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
2010-08-25 18:39:17
I like this. For completeness, what will happen if I pass an invalid month (like "Äugust")?
Pekka
2010-08-25 18:41:02
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
2010-08-25 18:42:48
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
2010-08-25 18:43:54
@Andy E, nice catches. I initially wrote it from memory, hadn't written tests yet. Should be good to go now though.
Chad
2010-08-25 18:49:31
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
2010-08-25 18:54:59
@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
2010-08-25 18:57:40
+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
2010-08-25 18:39:23
This is solid. Out of interest: how would I make this case insensitive? In case I need to parse a user input?
Pekka
2010-08-25 18:42:02
FWIW, this will obviously be faster if you're doing this many times.
Eric Wendelin
2010-08-25 18:42:08
generate the array in uppercase, and uppercase the user input before checking the array.
Fosco
2010-08-25 18:48:30
@Pointy, as mentioned, this has the downside of casing, and what if the month name were just **Aug** ?
Chad
2010-08-25 18:59:32
+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
2010-08-25 18:40:43
It could be replaced with Array.indexOf, but that isn't cross browser friendly.
CD Sanchez
2010-08-25 18:56:08
@palswim it seems that "ooo" is already using jQuery because many of his recent questions are thusly tagged.
Pointy
2010-08-25 18:58:18
@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
2010-08-25 18:59:46
A:
function monthToNumber(month) {
return new Date(Date.parse("1 "+month)).getMonth()+1;
}
CD Sanchez
2010-08-25 18:45:30