一、获取系统时间并转化成字符串(打印出来是这个格式:yyyy-MM-dd HH:mm:ss)
NSDate * dateXT = [NSDate date];
NSCalendar * calendarXT = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *compXT = [calendarXT components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:dateXT];
NSString * stringXT = [NSString stringWithFormat:@"%ld-%ld-%ld %ld:%ld:%ld",(long)compXT.year, (long)compXT.month, (long)compXT.day, (long)compXT.hour, (long)compXT.minute, (long)compXT.second];
二、带年份的时间差(打印出来的是带年的时间差)
//创建时间对象
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秒", (long)com.year, (long)com.month, (long)com.day, (long)com.hour, (long)com.minute, (long)com.second);
三、不带年份的时间差(打印出来的是几天几分几秒)
//创建时间对象
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;
#define ios8orLater [UIDevice currentDevice].systemVersion.floatValue>=8.0
if (ios8orLater) {
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
calendar = [NSCalendar autoupdatingCurrentCalendar];
}
//拿到时间差(后面的减去前面)
NSDateComponents *com = [calendar components:NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date0 toDate:date1 options:0];
//转化成秒(这样的时间可以转化成秒)
NSTimeInterval startToEndsec =(long)com.day *86400 + (long)com.hour *3600+(long)com.minute * 60+(long)com.second;
NSLog(@"相差%ld天%ld小时%ld分钟%ld秒", (long)com.day, (long)com.hour, (long)com.minute, (long)com.second);
四、时间计算 刚刚-几分钟前-几小时前
//时间计算 刚刚-几分钟前-几小时前
+(NSString *)compareCurrentTime:(NSString*)str{
//创建时间对象
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//系统时间的字符串
NSDate * dateXT = [NSDate date];
NSCalendar * calendarXT = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *compXT = [calendarXT components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:dateXT];
NSString * stringXT = [NSString stringWithFormat:@"%ld-%ld-%ld %ld:%ld:%ld",(long)compXT.year, (long)compXT.month, (long)compXT.day, (long)compXT.hour, (long)compXT.minute, (long)compXT.second];
//系统时间
NSDate *dateTimeXT = [formatter dateFromString:stringXT];
//发帖日期
NSDate *date0 = [formatter dateFromString:str];
//初始化日历
NSCalendar * calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
//拿到时间差(系统时间减去发帖时间)
NSDateComponents *com = [calendar components:NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date0 toDate:dateTimeXT options:0];
//转化成秒
NSTimeInterval timeInterval =(long)com.day *86400 + (long)com.hour *3600+(long)com.minute * 60+(long)com.second;
NSLog(@"时间是%f",timeInterval);
long temp = 0;
NSString *result;
//小于60 秒显示刚刚
if (timeInterval < 60) {
result = [NSString stringWithFormat:@"刚刚"];
}
//小于60分钟直接显示几分钟前
else if((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%ld分钟前",temp];
}
//小于24小时直接显示几小时前
else if((temp = timeInterval/(60*60)) <24){
result = [NSString stringWithFormat:@"%ld小时前",temp];
}
//小于7天显示几天前
else if((temp = timeInterval/(86400)) <7){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
//大于7天小于14天显示1周前
else if((temp = timeInterval/(86400*7)) <2){
result = [NSString stringWithFormat:@"%ld周前",temp];
}
else{
//直接显示发帖日期
NSArray * arrayNew = [str componentsSeparatedByString:@"-"];
//切日
NSArray * arrayday = [[arrayNew lastObject] componentsSeparatedByString:@" "];
result =[NSString stringWithFormat:@"%@.%@.%@",[arrayNew firstObject],arrayNew[1],[arrayday firstObject]];
}
return result;
}