how to Parse date String
i use this code to get informations from strings with this format "01-05-2011"
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd'-'MM'-'yyyy"];
// Your date represented as a NSDate
NSDate *dateDepart = [formatter dateFromString:daparatureFly.date];
NSDateComponents *comps = [[NSCalendar c开发者_JAVA百科urrentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:dateDepart];
Then when i take the month like this [comps day],[comps month] , i have 1 and 5 , i want to have 01, 05 . Any one can help me ? thanx
Those components are NSSintegers, they are not formatted. When you use it in string you can do as follows:
NSString *day = [NSString stringWithFormat:@"%02d", [comps day]];
Something akin to...
//UNTESTED
[NSString stringWithFormat:@"%02d", [comps day]];
精彩评论