iOS的NSString * 与 NSDate *的日期转换处理

iOS的NSString * 与 NSDate *的日期转换处理


方法3.时间比较两个时间比较
NSDateFormatter *format = [[NSDateFormatter alloc] init];

format.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSString *createdAtString = @"2016-7-16 09:33:22";

NSDate *createdAtDate = [format dateFromString:createdAtString];

// 其他时间

NSString *otherString =@"2016-7-16 21:53:22";

NSDate *otherDate = [format dateFromString:otherString];

// 获得NSCalendar

NSCalendar *calendar = nil;

if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {

calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];

}else{

calendar = [NSCalendar currentCalendar];

}

// 获得日期之间的间隔

NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

NSDateComponents *components = [calendar components:unit fromDate:createdAtDate toDate:otherDate options:0];

NSLog(@"%@",components);

你可能感兴趣的:(iOS的NSString * 与 NSDate *的日期转换处理)