// 時(shí)間比較
// 1.過(guò)去時(shí)間
NSString *str = @"2017-01-11 15:48:52 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH-mm-ss Z";
NSDate *date = [dateFormatter dateFromString:str];
NSLog(@"%@", date);
// 2.現(xiàn)在時(shí)間
NSDate *nowDate = [NSDate date];
// 比較兩個(gè)時(shí)間
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit type = NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond;
NSDateComponents *comp = [calendar components:type fromDate:date toDate:nowDate options:0];
NSLog(@"year = %ld month = %ld day = %ld hour = %ld minute = %ld second = %ld", (long)comp.year, (long)comp.month, (long)comp.day, (long)comp.hour, (long)comp.minute, (long)comp.second);