5

I have 3 variables;

startDate - Wed Apr 12 endDate - Wed Jun 11 timeDuration - 11.00 am from 11.00 pm. (this is a string, 11AM is the start time and 12PM is the end time)

1.) Now, i need to get the current date and time ( [NSDate date] ) and check if the current date is between the startDate and endDate and then check if the current time is between the timeDuration (as in between 11.00am from 11.00pm)

2.) If the current time is between the time duration (as in between 11.00 am from 11.00 pm) I need to calculate the time remaining to the following scenarios;

a.) If the time is 10am, i need to calculate the time to the start time which is 11.00am. (the answer is 1 hour).

b.) If the time is 11.45AM, since the time is greater than the start time (which was 11.00am) i need to calculate the time remaining to the end time (which is 11.00pm).

c.) If the time is 11.45pm, since the time is greater than the end time (which is 11.00pm), i need to print a NSLog saying that 'Time Exceeded'.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
Illep
  • 16,375
  • 46
  • 171
  • 302
  • possible duplicate of [How to Check if an NSDate occurs between two other NSDates](http://stackoverflow.com/questions/1072848/how-to-check-if-an-nsdate-occurs-between-two-other-nsdates), http://stackoverflow.com/questions/1987506, http://stackoverflow.com/questions/9807370/, http://stackoverflow.com/questions/8295008/ – jscs May 20 '12 at 08:18
  • What if the current time is 12:15am? Is it too late from yesterday, or too early today?? – lnafziger May 21 '12 at 02:51
  • What kind of variable is startDate? An NSDate or NSString? – lnafziger May 21 '12 at 02:55

2 Answers2

1

You can use - (NSComparisonResult)compare:(NSDate *)anotherDate
compare current date to startDate and endDate.

NSComparisonResult

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

Example:

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

   //currentDate is between startDate and endDate.
}

or take a look at this solution by bbum .

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
1

The basic idea is to convert your startDate and endDate into actual NSDate's and compare them to the current date. Then if the current date is in the proper range, you add the start and end times onto the current date to come up with a specific point in time for the starting and ending times. Once you have those dates, you can use the compare and timeIntervalSinceDate methods of NSDate to get the values that you need:

// 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);
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • But, if the `currentTime` is equal to the `startDateString` or `endDateString` it will execute the `if ([startDate compare:currentTime] == NSOrderedDescending....` block, and doesn't calculate the time remaining. How can i fix this ? – Illep May 23 '12 at 01:31
  • Change `== NSOrderedDescending` to `!= NSOrderedAscending` (which basically means `<=` instead of just `<`. – lnafziger May 23 '12 at 01:32
  • I require the `CurrentTime` to have the system date and time (now it displays GMT time), it is the same with startTime and endTime too. I need to display the Seconds of the time remaining to the endDateString ot startDateString. – Illep May 28 '12 at 16:05
  • It is in the current time (unless you are inspecting it in the debugger where it simply **shows** it in GMT). I calculated the time remaining in minutes for you. Just get rid of the "divide by 60" and you will have seconds.... – lnafziger May 29 '12 at 04:05