开发者

how to Parse date String

开发者 https://www.devze.com 2023-03-07 21:04 出处:网络
i use this code to get informations from strings with this format \"01-05-2011\" NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];

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]];
0

精彩评论

暂无评论...
验证码 换一张
取 消