Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios- Check whether current time is between two times or not

Tags:

ios

nsdate

I want to check whether the current time is between two time or not.

For example, I want to check if the current time is between today morning 10AM to 12PM or Tomorrow morning 10AM to 12PM. How can I do this?

like image 876
Dev Avatar asked Dec 20 '12 05:12

Dev


1 Answers

Try this

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger currentHour = [components hour];
NSInteger currentMinute = [components minute];
NSInteger currentSecond = [components second];

if (currentHour < 7 || (currentHour > 21 || currentHour == 21 && (currentMinute > 0 || currentSecond > 0))) {
    // Do Something
}

Replace 7, 21 with your time

I get this from How to compare time

like image 90
CRDave Avatar answered Oct 22 '22 04:10

CRDave