【问题标题】:Check if the time and date is between a particular date and time检查时间和日期是否在特定日期和时间之间
【发布时间】:2012-05-27 03:21:35
【问题描述】:

我有 3 个变量;

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

1.) 现在,我需要获取当前日期和时间 ([NSDate date]) 并检查当前日期是否在 startDateendDate 之间,然后检查当前时间是否在 @987654331 之间@(在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 说“超时”。

【问题讨论】:

标签: iphone objective-c ios nsdate nsdateformatter


【解决方案1】:

基本思想是将您的startDateendDate 转换为实际的NSDate,并将它们与当前日期进行比较。然后,如果当前日期在适当的范围内,则将开始和结束时间添加到当前日期,以得出开始和结束时间的特定时间点。获得这些日期后,您可以使用 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);

【讨论】:

  • 但是,如果currentTime 等于startDateStringendDateString,它将执行if ([startDate compare:currentTime] == NSOrderedDescending.... 块,并且不计算剩余时间。我该如何解决这个问题?
  • == NSOrderedDescending 更改为!= NSOrderedAscending(这基本上意味着<= 而不仅仅是<
  • 我需要CurrentTime 有系统日期和时间(现在它显示格林威治标准时间),它与startTime 和endTime 相同。我需要显示到 endDateString ot startDateString 的剩余时间的秒数。
  • 当前时间(除非您在调试器中检查它,它只是在 GMT 中显示)。我为您计算了剩余时间(以分钟为单位)。只要去掉“除以 60”,你就会有几秒钟的时间......
【解决方案2】:

您可以使用- (NSComparisonResult)compare:(NSDate *)anotherDate
将当前日期与 startDate 和 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.
}

或查看bbumthis 解决方案。

【讨论】:

    猜你喜欢
    • 2018-07-26
    • 2021-12-18
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2011-02-07
    相关资源
    最近更新 更多