NSCalendar 时间比较

NSCalendar 时间比较_第1张图片

// 时间比较

// 1.过去时间

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.现在时间

NSDate *nowDate = [NSDate date];

// 比较两个时间

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);

你可能感兴趣的:(NSCalendar 时间比较)