Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get month number from month name?

Quick question, I have a list of months by name i.e. "Jan" "Feb" "Mar" "Apr" ... etc. that I want to convert into 1,2,3,4 ... etc. I have written some simple code to do this but was just curious if anyone knew a way to do this using any of the APIs.

like image 570
fuzzygoat Avatar asked Feb 02 '12 15:02

fuzzygoat


People also ask

How do I convert month name to month number in Excel?

There are two Excel functions that can help you convert month names to numbers - DATEVALUE and MONTH. Excel's DATEVALUE function converts a date stored as text to a serial number that Microsoft Excel recognizes as a date. And then, the MONTH function extracts a month number from that date.

How do you find the number of months?

That is, it counts the day of the first date but not the ending date. To get around this, bump the date by one in the end. For example, June 1, 2000 to June 1, 2001 is less than twelve months. However, June 1, 2000 to June 2, 2001 is 12 months.

How do you convert a number to a month name?

Please do as follows: Select a blank cell next to the sales table, type the formula =TEXT(A2*29,"mmm") (Note: A2 is the first number of the Month list you will convert to month name), and then drag the AutoFill Handle down to other cells. Now you will see the numbers (from 1 to 12) are converted to normal month names.

How do I calculate the number of months in Excel?

Example 3. We can even use this function to convert month names to numbers. For this, we need to use two functions DATEVALUE & MONTH. The formula to use is =MONTH(DATEVALUE(A2 & “1”)).


2 Answers

Quick answer:

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MMM"];
NSDate *aDate = [formatter dateFromString:@"Jul"];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit fromDate:aDate];
NSLog(@"Month: %i", [components month]); /* => 7 */

See date formatters.

like image 122
djromero Avatar answered Sep 17 '22 14:09

djromero


Check out the NSDateComponents class Ref. That would be the class you might want to consider using.

like image 23
Just a coder Avatar answered Sep 18 '22 14:09

Just a coder