NSDate between two given NSDates

This isn’t perfect, but you could use [NSDate compare:] to check your date against both boundaries: NSDate *firstDate = … NSDate *secondDate = … NSDate *myDate = [NSDate date]; switch ([myDate compare:firstDate]) { case NSOrderedAscending: NSLog(@”myDate is older”); // do something break; case NSOrderedSame: NSLog(@”myDate is the same as firstDate”); // do something break; case … Read more

Convert date string swift

The standard ISO8601 date format with fractional seconds and time zone is yyyy-MM-dd’T’HH:mm:ss.SSSZ let myDate = “2016-06-20T13:01:46.457+02:00” let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: “en_US_POSIX”) // edited dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ss.SSSZ” let date = dateFormatter.dateFromString(myDate)! dateFormatter.dateFormat = “dd/MM/yyyy” let dateString = dateFormatter.stringFromDate(date) Swift 3: let myDate = “2016-06-20T13:01:46.457+02:00” let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: “en_US_POSIX”) … Read more

NSTimer not firing when runloop is blocked

bbum’s answer provides a better way to design your application, but if you want your timer to fire regardless of whether the user is manipulating the UI or not you’ll need to add it to the tracking mode of the runloop. Assuming that you’re developing for the iPhone, that mode is UITrackingRunLoopMode. If you’re developing … Read more

iOS: Compare two NSDate-s without time portion

NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger comps = (NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear); NSDateComponents *date1Components = [calendar components:comps fromDate: date1]; NSDateComponents *date2Components = [calendar components:comps fromDate: date2]; date1 = [calendar dateFromComponents:date1Components]; date2 = [calendar dateFromComponents:date2Components]; NSComparisonResult result = [date1 compare:date2]; if (result == NSOrderedAscending) { } else if (result == NSOrderedDescending) { } else { … Read more

UIDatePicker, setting maximum and minimum dates based on todays date

Not tested, but you probably want something like this. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *currentDate = [NSDate date]; NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:30]; NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0]; [comps setYear:-30]; NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0]; [datePicker setMaximumDate:maxDate]; [datePicker setMinimumDate:minDate]; Update for Swift 4.1 let calendar = … Read more

How do I get hour and minutes from NSDate?

Use an NSDateFormatter to convert string1 into an NSDate, then get the required NSDateComponents: Obj-C: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@”<your date format goes here”]; NSDate *date = [dateFormatter dateFromString:string1]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date]; NSInteger hour = [components hour]; NSInteger minute = [components minute]; … Read more