我列出了当月订单中的生日列表(8月,9月...... 7月)
我已经计算了当前日期和生日列表日期之间的天数。
在ViewDidLoad方法
中NSDateFormatter *curdatemonthnoformatter = [[NSDateFormatter alloc] init];
[curdatemonthnoformatter setDateStyle:NSDateFormatterMediumStyle];
[curdatemonthnoformatter setDateFormat:@"dd-MM"];
NSString * curdatemonthnostring = [curdatemonthnoformatter stringFromDate:[NSDate date]];
[curdatemonthnoformatter release];
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd-MM"];
NSDate *startdate = [tempFormatter dateFromString:curdatemonthnostring];
NSLog(@"%@",startdate);
[tempFormatter release];
NSLog(@"%@",curdatemonthnostring);
for (int val=0; val<[monthlistArray count]; val++)
{
NSMutableArray * dataarraylocal = [[[NSMutableArray alloc]initWithCapacity:0]autorelease];
for (int ival=0; ival<[[monthdataArray objectAtIndex:val]count]; ival++)
{
NSString *birthdatestr=[NSString stringWithFormat:@"%@",[[[monthdataArray objectAtIndex:val]objectAtIndex:ival]objectForKey:@"birthday"]];
NSDate *mybirthdate=[formatter dateFromString:birthdatestr];
NSDateFormatter *birthdatemonthFormatter = [[NSDateFormatter alloc] init];
[birthdatemonthFormatter setDateStyle:NSDateFormatterMediumStyle];
[birthdatemonthFormatter setDateFormat:@"dd-MM"];
NSString *sortmonthstr = [birthdatemonthFormatter stringFromDate:mybirthdate];
NSDateFormatter *tempFormatter1 = [[[NSDateFormatter alloc]init]autorelease];
[tempFormatter1 setDateFormat:@"dd-MM"];
NSDate *toDate = [tempFormatter1 dateFromString:sortmonthstr];
[dataarraylocal addObject:toDate];
}
[previouscomparearray addObject:dataarraylocal];
}
NSLog(@"%@",previouscomparearray);
for (int ival=0; ival<[monthlistArray count]; ival++)
{
NSMutableArray *nofdayslocal=[[NSMutableArray alloc]initWithCapacity:0];
for (int jval=0; jval<[[monthdataArray objectAtIndex:ival]count]; jval++)
{
unsigned flags = NSDayCalendarUnit;
NSDateComponents *difference = [[NSCalendar currentCalendar] components:flags fromDate:startdate toDate:[[previouscomparearray objectAtIndex:ival]objectAtIndex:jval] options:0];
int dayDiff = [difference day];
NSLog(@"%d",dayDiff);
[nofdayslocal addObject:[NSNumber numberWithInt:dayDiff]];
}
[comparedatearray addObjectsFromArray:nofdayslocal];
}
NSLog(@"%@",comparedatearray);
输出:
"-8",
"-5",
"-1",
31,
83,
115,
"-220",
"-154",
"-154",
"-130",
"-90",
"-80",
"-73",
"-68",
"-63",
"-44",
"-42"
它在当前年份内计算。我想在12月之后根据明年计算。
任何想法请帮助我。
答案 0 :(得分:0)
您可以找到日期和时间编程的完整指南。这是一个链接(https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1),可以为您提供帮助。
NSString *first = @"2010-10-09";
NSString *second = @"2011-12-10";
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [form dateFromString:first];
NSDate *endDate = [form dateFromString:second];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorianCalendar components:NSDayCalendarUnit
fromDate:startDate
toDate:endDate
options:0];
comp会有天数的差异。你可以打印和检查。祝好运。 :)