24小时相隔30分钟 - NSDate

时间:2014-04-17 23:50:43

标签: objective-c nsmutablearray nsdate nscalendar nsdatecomponents

我试图获取NSMutableArrayNSDateNSDate个对象,从最近的24小时开始,最近四舍五入为HH:00或HH:30

现在就说吧02:31我想输出@ [03:00,03:30,04:00,04:30 ... 03:00(第二天)]

如果是02:29那么我想要@ [02:30,03:00等等,直到第二天02:30]。

现在我有下面的代码,它几乎一直都在工作,但很少我得到所有NSDate对象的输出我想要剩余的额外分钟(@ [03:01, 03:31:04:01 ......等等]。

任何想法?

-(NSMutableArray*)TwentyFourHoursFromNowDivicedBy30Mins
{

    NSMutableArray * dateArray = [NSMutableArray array];
    NSCalendar * cal = [NSCalendar currentCalendar];

    NSDateComponents * plusDays = [NSDateComponents new];
    NSDate * now = [self changeTimeValue:[NSDate date]];

    for( NSUInteger day = 0; day < 2; day++ )
    {
        [plusDays setDay:day];
        [dateArray addObject:[cal dateByAddingComponents:plusDays toDate:now options:0]];
    }

    NSDate *startDate = [dateArray objectAtIndex:0];
    NSDate *endDate = [dateArray objectAtIndex:1];
    NSDateComponents *diff = [[NSDateComponents alloc] init];
    [diff setMinute:0];
    NSCalendar *calz = [NSCalendar currentCalendar];
    NSDate *tmp = startDate;

    NSMutableArray *dates = [NSMutableArray arrayWithObject:tmp];

    while ([tmp laterDate:endDate] == endDate) {
        [diff setMinute:[diff minute] + 30];
        tmp = [calz dateByAddingComponents:diff toDate:startDate options:0];
        [dates addObject:tmp];

    }


    return dates;

}
- (NSDate *)changeTimeValue:(NSDate *)dateValue{
    NSDateComponents *time = [[NSCalendar currentCalendar]
                              components:NSHourCalendarUnit | NSMinuteCalendarUnit
                              fromDate:dateValue];
    long val = 0;
    NSDate *newDate = [[NSDate alloc] init];
    NSInteger minutes = [time minute];
    if(minutes > 0 && minutes < 30) {
        val = 30 - minutes; NSTimeInterval aTimeInterval = [dateValue
                                                            timeIntervalSinceReferenceDate] + 60 * val + minutes;
        newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
        return newDate;
    } else if(minutes > 30 && minutes < 60) {
        val = 60 - minutes;
        NSTimeInterval aTimeInterval = [dateValue timeIntervalSinceReferenceDate]
        + 60 * val;
        newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
        return newDate;
    } else {
        return newDate;
    }
}

1 个答案:

答案 0 :(得分:0)

你没有将changeTimeValue中的秒数归零,无论如何我觉得你过于复杂了。所以我怀疑你后来看到了四舍五入的问题。只需使用NSDateComponents完成整个工作:

NSCalendar *currentCalendar = [NSCalendar currentCalendar];

NSDateComponents *dateComponents = [currentCalendar
                              components:NSHourCalendarUnit | 
                                         NSMinuteCalendarUnit | 
                                         NSYearCalendarUnit |
                                         NSMonthCalendarUnit |
                                         NSDayCalendarUnit
                              fromDate:dateValue];

dateComponents.minute += 15;
dateComponents.minute -= dateComponents.minute%30;

return [currentCalendar dateFromComponents:dateComponents];