5

我有 3 个变量;

开始日期 -Wed Apr 12 结束日期 -Wed Jun 11 时间持续时间 - 11.00 am from 11.00 pm。(这是一个字符串,11AM 是开始时间,12PM 是结束时间)

1.) 现在,我需要获取当前日期和时间 ( [NSDate date]) 并检查当前日期是否介于startDateand之间endDate,然后检查当前时间是否介于timeDuration(as in between 11.00am from 11.00pm)

2.)如果当前时间在持续时间之间(如在之间11.00 am from 11.00 pm)我需要计算剩余时间到以下场景;

a.) 如果时间是上午 10 点,我需要计算到开始时间的时间,即11.00am。(答案是1 hour)。

b.)如果时间是11.45AM,由于时间大于开始时间(这是11.00am)我需要计算剩余时间到结束时间(这是11.00pm)。

c.) 如果时间是11.45pm,由于时间大于结束时间(即11.00pm),我需要打印一个 NSLog 说“超时”。

4

2 回答 2

1

您可以使用- (NSComparisonResult)compare:(NSDate *)anotherDate
比较当前日期来开始日期和结束日期。

NSComparisonResult

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};    

例子:

NSDate *currentDate = [NSDate date];  
if (([currentDate compare:startDate ] == NSOrderedDescending) &&
    ([currentDate compare:endDate ] == NSOrderedAscending)) {

   //currentDate is between startDate and endDate.
}

或通过bbum查看解决方案。

于 2012-05-20T07:17:10.827 回答
1

基本思想是将您的startDateandendDate转换为实际NSDate的 's 并将它们与当前日期进行比较。然后,如果当前日期在适当的范围内,则将开始和结束时间添加到当前日期,以得出开始和结束时间的特定时间点。一旦你有了这些日期,你就可以使用 NSDate 的comparetimeIntervalSinceDate方法来获取你需要的值:

// NOTE:  NO error checking is being done in this code.

// Starting variables  
NSString *startDateString     = @"Wed Apr 12";
NSString *endDateString       = @"Wed Jun 11";
NSString *timeDurationString = @"11.00 am from 11.00 pm";

// Get the current date
NSDate *currentTime        = [NSDate date];
NSCalendar *cal            = [NSCalendar currentCalendar];
NSDateComponents 
         *currentDateComps = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
                                    fromDate:currentTime];

// Add the current year onto our date string
NSString *startDateStringWithYear = [NSString stringWithFormat:@"%@ %d", startDateString, currentDateComps.year];
NSString *endDateStringWithYear   = [NSString stringWithFormat:@"%@ %d", endDateString, currentDateComps.year];

// Calculate NSDate's for start/endDate
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MM dd yyyy"];
NSDate *startDate = [formatter dateFromString:startDateStringWithYear];
NSDate *endDate   = [formatter dateFromString:endDateStringWithYear];

// Return if today's date is not between the start and end dates
if ([startDate compare:currentTime] == NSOrderedDescending || 
    [endDate compare:currentTime] == NSOrderedAscending) 
{
    return;
}

// Break the timeDurationString into separate words
NSArray *timeDurationStringWords = [timeDurationString componentsSeparatedByString:@" "];

// Calculate NSDate's for the start time for today:
NSString *startTimeString = [timeDurationStringWords objectAtIndex:0];
currentDateComps.hour     = [[startTimeString substringToIndex:2] intValue];
currentDateComps.minute   = [[startTimeString substringFromIndex:3] intValue];
if ([[timeDurationStringWords objectAtIndex:1] isEqualToString:@"pm"]) 
{
    currentDateComps.hour += 12;
}
NSDate *startTime         = [cal dateFromComponents:currentDateComps];

// And now the end time for today
NSString *endTimeString   = [timeDurationStringWords objectAtIndex:3];
currentDateComps.hour     = [[endTimeString substringToIndex:2] intValue];
currentDateComps.minute   = [[endTimeString substringFromIndex:3] intValue];
if ([[timeDurationStringWords objectAtIndex:4] isEqualToString:@"pm"]) 
{
    currentDateComps.hour += 12;
}
NSDate *endTime           = [cal dateFromComponents:currentDateComps];

// Now we have what we really want:  A specific start time, current time, and end time.

// Check to see if we are waiting to start:
if ([startTime compare:currentTime] == NSOrderedDescending) {
    NSTimeInterval minutesToStartTime = [startTime timeIntervalSinceDate:currentTime] / 60;
    NSLog(@"Start time is in %02d+%02d", (int)(minutesToStartTime / 60), (int)minutesToStartTime % 60);
    return;
}

// Check to see if we are late:
if ([endTime compare:currentTime] == NSOrderedAscending) {
    NSLog(@"Time Exceeded");
    return;
}

// We are between the two times:
NSTimeInterval minutesToEndTime = [endTime timeIntervalSinceDate:currentTime] / 60;
NSLog(@"End time is in %02d+%02d", (int)(minutesToEndTime / 60), (int)minutesToEndTime % 60);
于 2012-05-21T04:06:00.400 回答