时间转换成昨天,今天,明天的形式
Posted ShineYangGod
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了时间转换成昨天,今天,明天的形式相关的知识,希望对你有一定的参考价值。
* 仿QQ空间时间显示
* @param string eg:2015年5月24日 02时21分30秒
*/
- (NSString *)format:(NSString *)string
NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
[inputFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
NSDate*inputDate = [inputFormatter dateFromString:string];
//NSLog(@"startDate= %@", inputDate);
NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//get date str
NSString *str= [outputFormatter stringFromDate:inputDate];
//str to nsdate
NSDate *strDate = [outputFormatter dateFromString:str];
//修正8小时的差时
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: strDate];
NSDate *endDate = [strDate dateByAddingTimeInterval: interval];
//NSLog(@"endDate:%@",endDate);
NSString *lastTime = [self compareDate:endDate];
NSLog(@"lastTime = %@",lastTime);
return str;
-(NSString *)compareDate:(NSDate *)date
NSTimeInterval secondsPerDay = 24 * 60 * 60;
//修正8小时之差
NSDate *date1 = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date1];
NSDate *localeDate = [date1 dateByAddingTimeInterval: interval];
//NSLog(@"nowdate=%@\\nolddate = %@",localeDate,date);
NSDate *today = localeDate;
NSDate *yesterday,*beforeOfYesterday;
//今年
NSString *toYears;
toYears = [[today description] substringToIndex:4];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
beforeOfYesterday = [yesterday dateByAddingTimeInterval: -secondsPerDay];
// 10 first characters of description is the calendar date:
NSString *todayString = [[today description] substringToIndex:10];
NSString *yesterdayString = [[yesterday description] substringToIndex:10];
NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];
NSString *dateString = [[date description] substringToIndex:10];
NSString *dateYears = [[date description] substringToIndex:4];
NSString *dateContent;
if ([dateYears isEqualToString:toYears]) //同一年
//今 昨 前天的时间
NSString *time = [[date description] substringWithRange:(NSRange)11,5];
//其他时间
NSString *time2 = [[date description] substringWithRange:(NSRange)5,11];
if ([dateString isEqualToString:todayString])
dateContent = [NSString stringWithFormat:@"今天 %@",time];
return dateContent;
else if ([dateString isEqualToString:yesterdayString])
dateContent = [NSString stringWithFormat:@"昨天 %@",time];
return dateContent;
else if ([dateString isEqualToString:beforeOfYesterdayString])
dateContent = [NSString stringWithFormat:@"前天 %@",time];
return dateContent;
else
return time2;
else
return dateString;
以上是关于时间转换成昨天,今天,明天的形式的主要内容,如果未能解决你的问题,请参考以下文章