连接两个NSDate字符串值

时间:2012-06-19 08:31:04

标签: iphone objective-c

我只是感到困惑的是,将11-jan-2012和15-jan-2012两个NSDate值连接为11-15-jan-2012有任何默认功能。 同样是11-jan-2012和15-jul-2012,作为11-jan-15-jul-2012。 Thx提前。

2 个答案:

答案 0 :(得分:2)

你很容易..你可以得到日,月和年

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger dayM = [components day];    
NSInteger monthM = [components month];
NSInteger yearM = [components year];

您可以从中制作这些日期,只需使用您的格式更改NSDate日期即可 然后根据需要每天结束

答案 1 :(得分:1)

你可以使用这段代码

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MMM-yyyy"];
NSDate *date1 = [formatter dateFromString:@"11-jan-2012"];
NSDate *date2 = [formatter dateFromString:@"15-jan-2012"];

NSDateComponents *dateComponents;
dateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit  
                                                 fromDate:date1];
int day1 = [dateComponents day];    
int month1 = [dateComponents month];
int year1 = [dateComponents year];

dateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit  
                                                 fromDate:date2];
int day2 = [dateComponents day];    
int month2 = [dateComponents month];
int year2 = [dateComponents year];

NSString *str = [NSString stringWithFormat:@"%d - %d %d %d", day1, day2, month1, year1];