1

我试图在 iOS 上找到一周内即将到来的生日。我从 Facebook 中提取了生日数据并修剪了年份部分(因为根据用户的隐私设置,某些用户可能无法使用它)因此生日数据是NSString带日期格式的@"MM/dd"

NSDateFormatter *dateFormatter = [[NSDateFormatter dateFormatterWithFormat:@"MM/dd"] retain];
NSDate *today = [NSDate date];
NSDateComponents *todayComponents = [dateFormatter.calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                              fromDate:today];
todayComponents.hour = 13;
todayComponents.minute = 0;
todayComponents.second = 0;
today = [dateFormatter.calendar dateFromComponents:todayComponents];

[facebookFriends enumerateObjectsUsingBlock:^(NSDictionary *friend,
                                              NSUInteger idx,
                                              BOOL *stop) {

    NSDate *birthday = [dateFormatter dateFromString:[friend objectForKey:@"birthday"]];
    NSDateComponents *birthdayComponents = [dateFormatter.calendar components:NSMonthCalendarUnit | NSDayCalendarUnit
                                                                     fromDate:birthday];
    birthdayComponents.year = todayComponents.year;
    birthdayComponents.hour = todayComponents.hour;
    birthdayComponents.minute = todayComponents.minute;
    birthdayComponents.second = todayComponents.second;

    birthday = [dateFormatter.calendar dateFromComponents:birthdayComponents];
    NSDateComponents *difference = [dateFormatter.calendar components:NSDayCalendarUnit
                                                             fromDate:today
                                                               toDate:birthday
                                                              options:0];
    if (difference.day >= 0 &&
        difference.day < 7) { // 7 days in a week
        [birthdaysToShow[difference.day] addObject:friend];
    }
}];
[dateFormatter release];

dateFormatterWithFormat:的类别在哪里NSDateFormatter

+ (NSDateFormatter *)dateFormatterWithFormat:(NSString *)formatString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    dateFormatter.calendar = calendar;
    dateFormatter.dateFormat = formatString;
    dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

    return [dateFormatter autorelease];
}

这里有2个问题:

1-我在这段代码中有一个错误,如果距离新年不到一周,我将无法显示新年的生日,因为我将生日的年份设置为当年和difference.day新年的生日将远小于 0。我该如何解决这个问题?

2- 正如你从上面的代码片段中看到的那样,我做了很多跳舞:首先我得到今天的日期和时间[NSDate date],然后我把它撕成它的组件并将它组装回一个NSDate中午的时间而不是当前的时间时间。我这样做的原因是如果[NSDate date]返回一个时间是 23:00:00 的日期,并且如果生日时间是 00:00:00(1 小时后,意味着生日是明天),那么difference.day等于0而不是想要的1。我在上面的代码片段中所做的是正确的方法吗?我想我可能为看似如此简单的事情做了太多的工作,你将如何解决这种安排,这样我就不必将组件分割成组件today并将birthday它们放回NSDate?

4

1 回答 1

1

为每个生日组件对象设置正确的年份:

birthdayComponents.year=todayComponents.year; //default

if(birthdayComponents.month< todayComponents.month) birthdayComponents.year=todayComponents.year+1; //their next birthday is next year.

if(birthdayComponents.month==todayComponents.month && birthdayComponents.day<todayComponents.day) birthdayComponents.year=todayComponents.year+1; //their next birthday is next year
于 2013-02-28T19:52:12.797 回答