一夺克、獲取系統(tǒng)時間并轉化成字符串(打印出來是這個格式: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];
二、帶年份的時間差(打印出來的是帶年的時間差)
//創(chuàng)建時間對象
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);
三、不帶年份的時間差(打印出來的是幾天幾分幾秒)
//創(chuàng)建時間對象
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{
//創(chuàng)建時間對象
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//系統(tǒng)時間的字符串
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];
//系統(tǒng)時間
NSDate *dateTimeXT = [formatter dateFromString:stringXT];
//發(fā)帖日期
NSDate *date0 = [formatter dateFromString:str];
//初始化日歷
NSCalendar * calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
//拿到時間差(系統(tǒng)時間減去發(fā)帖時間)
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{
//直接顯示發(fā)帖日期
NSArray * arrayNew = [str? componentsSeparatedByString:@"-"];
//切日
NSArray * arrayday = [[arrayNew lastObject]? componentsSeparatedByString:@" "];
result =[NSString stringWithFormat:@"%@.%@.%@",[arrayNew firstObject],arrayNew[1],[arrayday firstObject]];
}
return result;
}