- NSDate时间间隔的简单整理
- 当然用到的时候一般都会配合分类来使用,如先判断是否为今年,今天,昨天等,在进行时间差的比较,当然比较时间差代码也可以封装到分类,这里就不一一说明,只是简单记录下
代码如下
//创建时间对象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *str = @"2014-05-13 21:34:55";
NSDate *date = [formatter dateFromString:str];
NSString *str1 = @"2016-02-8 22:49:33";
NSDate *date1 = [formatter dateFromString:str1];
//初始化日历
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
//拿到时间差
NSDateComponents *com = [calendar components:NSCalendarUnitYear |NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date toDate:date1 options:0];
NSLog(@"相差%ld年%ld个月%ld天%ld小时%ld分钟%ld秒", com.year, com.month, com.day, com.hour, com.minute, com.second);
时区转换:http://blog.csdn.net/lioneldy/article/details/51556931