Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the time and date is between a particular date and time

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'.

like image 777
Illep Avatar asked Nov 04 '22 23:11

Illep


2 Answers

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);
like image 141
lnafziger Avatar answered Nov 13 '22 03:11

lnafziger


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 .

like image 20
Parag Bafna Avatar answered Nov 13 '22 05:11

Parag Bafna