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.
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.
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.
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.
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”)).
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.
Check out the NSDateComponents class Ref. That would be the class you might want to consider using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With