2011-05-24 118 views
-1

我用这个代码从字符串信息与格式“2011年1月5日”如何解析日期字符串

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 currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
                   fromDate:dateDepart]; 

然后当我把这样的[谱曲日]月[谱曲月],我有1和5,我想要有01,05。任何人都可以帮助我? thanx

+0

请奖励谁在过去帮助过你的人和[接受一些答案](HTTP ://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。谢谢。 – DarkDust 2011-05-24 13:38:58

+0

@DarkDust我alwayse接受答案!为什么你告诉我这个 – user761812 2011-05-24 13:49:23

+0

因为有了这个问题,你已经接受了你十个问题中的两个问题的答案。和你的一些问题有可能被接受的有效答案:[1](http://stackoverflow.com/questions/6073855/how-to-add-description-to-the-uitableview),[2](http ://stackoverflow.com/questions/6075696/how-to-make-a-rotate-animation),[3](http://stackoverflow.com/questions/6066309/put-attribute-of-one-object-在-型动物-行)。 – DarkDust 2011-05-24 14:06:24

回答

2

这些组件是NSSintegers,它们没有格式化。当您在字符串中使用它,你可以做如下:

NSString *day = [NSString stringWithFormat:@"%02d", [comps day]]; 
+0

非常感谢 – user761812 2011-05-24 13:48:51

0

一种近乎...

//UNTESTED 
[NSString stringWithFormat:@"%02d", [comps day]];