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