Quick question, I have a list of months named "Jan" "Feb" "Mar" "Apr" ... etc. that I want to convert to 1,2,3,4 ... etc . I wrote simple code for this, but it was just curious if anyone knew about this using any of the APIs.
Quick response:
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 .
See the NSDateComponents Ref class. This will be the class you might want to consider.
The easiest way to get a number is to get an index, which if the list of months is ordered:
NSLog(@"The number of the month is: %d.",[listOfMonths indexOfObject:@"Jan"]+1);
There is nothing more direct than that.
For the Swift user, you can use:
Calendar.current.component(.month, from: date) Calendar.current.component(.day, from: date) ...
Source: https://habr.com/ru/post/1394412/More articles:seam file upload to postgres bytea column "column istete, but expression is of type bigint" - postgresqlHow to clear www cache? - xmlBest practice to avoid SQL injection vulnerability in SQL Server - ASP.Net - sql-injectionIs the instruction pointer a program visible register? - x86How can I access a static resource in webapp from a JUnit test? - javapackage for copying files using xcopy - batch-fileSelect row with excellent value and maximum date - mysqlStreaming audio using Python (without GStreamer) - pythonAny difference between .innerHTML and .set ('html', '') in mootools? - mootoolsSQL injection in django application - sqlAll Articles