2012-05-20 9 views
5

Mam 3 zmienne;Sprawdź, czy godzina i data znajdują się między konkretną datą i godziną.

startDate - Wed Apr 12 endDate - Wed Jun 11 timeDuration - 11.00 am from 11.00 pm. (to jest ciąg, 11AM to czas rozpoczęcia, a 12PM to czas końcowy)

1.) Teraz potrzebuję uzyskać aktualną datę i czas ([NSDate date]) i sprawdzić, czy aktualna data jest pomiędzy startDate a endDate a następnie sprawdzić, czy aktualny czas jest między timeDuration (jak w między 11.00am from 11.00pm)

2.) Jeśli aktualny czas jest między okres czasu (jak w między 11.00 am from 11.00 pm) muszę obliczyć czas pozostały do ​​następujących scenariusze;

a.) Jeśli czas jest 10 rano, muszę obliczyć czas do czasu rozpoczęcia, który jest 11.00am. (odpowiedź brzmi: 1 hour).

b). Jeżeli czas 11.45AM, ponieważ czas jest większy od czasu początkowego (który był 11.00am), trzeba obliczyć czas pozostały do ​​zakończenia (co 11.00pm).

c). Jeśli czas jest 11.45pm, ponieważ czas jest większy niż czas zakończenia (co jest 11.00pm), muszę wydrukować NSLog mówiąc, że „Czas przekroczony”.

+0

możliwe duplikat [Jak sprawdzić, jeśli NSDate występuje między dwoma innymi 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/ –

+0

Co jeśli aktualny czas to 12:15? Czy jest za późno, wczoraj, czy dziś za wcześnie? – lnafziger

+0

Jaką zmienną jest data rozpoczęcia? NSDate lub NSString? – lnafziger

Odpowiedz

1

Podstawową ideą jest przekształcenie startDate i endDate w rzeczywistą NSDate i porównanie ich z bieżącą datą. Następnie, jeśli aktualna data jest w odpowiednim zakresie, dodajesz czas rozpoczęcia i zakończenia do bieżącej daty, aby podać konkretny moment w czasie początkowym i końcowym. Gdy masz te daty, można użyć compare i timeIntervalSinceDate metod NSDate w celu uzyskania wartości, które trzeba:

// 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); 
+0

Ale jeśli 'currentTime' jest równe' startDateString' lub 'endDateString', to wykona blok' if ([startDate compare: currentTime] == NSOrderedDescending .... 'i nie oblicza pozostałego czasu Jak mogę to naprawić? – Illep

+0

Zmień '== NSOrderedDescending' na'! = NSOrderedAscending' (co oznacza po prostu '<=' zamiast tylko '<'. – lnafziger

+0

Czy to ci pomogło? – lnafziger

1

Możesz użyć - (NSComparisonResult)compare:(NSDate *)anotherDate
porównać bieżącą datę z datą rozpoczęcia i datą końcową.

NSComparisonResult 

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

przykład:

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

    //currentDate is between startDate and endDate. 
} 

lub przyjrzeć this roztworu przez bbum.

+0

Czy możesz pokazać mi przykład? – Illep

+0

Ale jak mogę pokazać czas (pytanie 2)? – Illep