检查时间和date是否在特定date和时间之间
我有3个variables;
开学date – Wed Apr 12
– Wed Jun 11
11.00 am from 11.00 pm
上课时间 – 11.00 am from 11.00 pm
。 (这是一个string,11AM是开始时间,12PM是结束时间)
1.)现在,我需要获取当前date和时间( [NSDate date]
),并检查当前date是否介于startDate
和endDate
之间,然后检查当前时间是否在timeDuration
之间(如11.00am from 11.00pm
)
2.)如果当前时间在持续时间之间(在11.00 am from 11.00 pm
),我需要计算下列情况的剩余时间;
a)如果时间是上午10点,我需要计算开始时间的11.00am
时间。 (答案是1 hour
)。
b)如果时间是11.00am
时11.45AM
,由于时间大于开始时间( 11.00am
),我需要计算剩余时间到结束时间(即11.00pm
)。
c)如果时间是11.45pm
时11.45pm
,由于时间大于结束时间(即11.00pm
),我需要打印一个NSLog,说'超时'。
基本的想法是将您的startDate
和endDate
转换为实际的NSDate
,并将它们与当前date进行比较。 然后,如果当前date在适当的范围内,则将开始和结束时间添加到当前date,以便为开始和结束时间提供特定的时间点。 一旦你有了这些date,你可以使用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);
你可以使用- (NSComparisonResult)compare:(NSDate *)anotherDate
比较当前datestartDate和endDate。
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来看看这个解决scheme。