下面是我的代码,我会解释其中的内容
NSDateFormatter *formater = [[NSDateFormatter alloc] init] ;
[formater setDateFormat:@"MM-dd"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSDayCalendarUnit | NSMonthCalendarUnit);
NSDateComponents *firstComponents = [calendar
components:desiredComponents fromDate:fDate];
NSDateComponents *secondComponents = [calendar
components:desiredComponents fromDate:eDate];
NSDateComponents *thirdComponents = [calendar
components:desiredComponents fromDate:mdate];
NSDate *from = [calendar dateFromComponents:firstComponents];
NSDate *end = [calendar dateFromComponents:secondComponents];
NSDate *middle = [calendar dateFromComponents:thirdComponents];
NSString * froms = [formater stringFromDate:from];
NSLog(@"sringfroms:%@",froms);
NSLog(@"datefrom:%@",from);
NSString * ends = [formater stringFromDate:from];
NSLog(@"sringends:%@",ends);
NSLog(@"dateend:%@",end);
NSString * middles = [formater stringFromDate:from];
NSLog(@"sringmiddles:%@",middles);
NSLog(@"datemiddle:%@",middle);
这就是打印出来的。
sringfroms:06-30
datefrom:0001-06-29 18:06:32 +0000
sringends:06-30
dateend:0001-08-02 18:06:32 +0000
sringmiddles:06-30
datemiddle:0001-07-14 18:06:32 +0000
您可以看到我正在从日期中删除年份,该日期以字符串形式打印,但是当我创建日期时,它会自动使用 0001。我正在使用 kal 日历,我只想标记月份和日期,所以它应该被标记为每年
if (([middle compare:from] == NSOrderedDescending)
&&([middle compare:end] == NSOrderedAscending)) {
[holidays addObject:[Holiday holidayNamed:naMMe
birthDate:bDate iDs:aIDDS date:middle]];
}
任何帮助将不胜感激