首先介绍基础文章,感谢各位的细心整理:
http://www.cnblogs.com/CCSSPP/p/3183410.html
http://my.oschina.net/yongbin45/blog/156181
http://tool.chinaz.com/Tools/unixtime.aspx
这东西看着简单,做起来也简单,主要是自己不懂给自己找借口说不好做。花了2小时才搞定。
需求详细:
外层选择界面
·今天显示格式 11:11
·昨天显示格式 昨天
·一星期内 星期X
·一星期前 2015/1/10
聊天界面
·今天显示格式 11:11
·昨天显示格式 昨天 11:11
·一星期内 星期X 11:11
·一星期前 2015/1/10 11:11
实现方法:
主要需要了解NSDateFormatter,NSCalendar,NSDateComponents这三个类的功能
直接上代码
在.h文件里面写一个调用方法
+(NSString *)getMessageDateStringFromTimeInterval:(NSTimeInterval)TimeInterval andNeedTime:(BOOL)needTime;
在.m文件里面写其他的相关方法
+(NSString *)getMessageDateStringFromTimeInterval:(NSTimeInterval)TimeInterval andNeedTime:(BOOL)needTime
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:TimeInterval];
return [MTChatTimeFormateTool getMessageDateString:date andNeedTime:needTime];
}
+ (NSString*)getMessageDateString:(NSDate*)messageDate andNeedTime:(BOOL)needTime
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale systemLocale]];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:@"YYYY/MM/dd HH:mm"];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:messageDate];
NSDate *msgDate = [cal dateFromComponents:components];
NSString*weekday = [MTChatTimeFormateTool getWeekdayWithNumber:components.weekday];
components = [cal components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
if([today isEqualToDate:msgDate]){
if (needTime) {
[formatter setDateFormat:@"今天 HH:mm"];
}
else{
[formatter setDateFormat:@"今天"];
}
return [formatter stringFromDate:messageDate];
}
components.day -= 1;
NSDate *yestoday = [cal dateFromComponents:components];
if([yestoday isEqualToDate:msgDate]){
if (needTime) {
[formatter setDateFormat:@"昨天 HH:mm"];
}
else{
[formatter setDateFormat:@"昨天"];
}
return [formatter stringFromDate:messageDate];
}
for (int i = 1; i <= 6; i++) {
components.day -= 1;
NSDate *nowdate = [cal dateFromComponents:components];
if([nowdate isEqualToDate:msgDate]){
if (needTime) {
[formatter setDateFormat:[NSString stringWithFormat:@"%@ HH:mm",weekday]];
}
else{
[formatter setDateFormat:[NSString stringWithFormat:@"%@",weekday]];
}
return [formatter stringFromDate:messageDate];
}
}
while (1) {
components.day -= 1;
NSDate *nowdate = [cal dateFromComponents:components];
if ([nowdate isEqualToDate:msgDate]) {
if (!needTime) {
[formatter setDateFormat:@"YYYY/MM/dd"];
}
return [formatter stringFromDate:messageDate];
break;
}
}
}
//1代表星期日、如此类推
+(NSString *)getWeekdayWithNumber:(int)number
{
switch (number) {
case 1:
return @"星期日";
break;
case 2:
return @"星期一";
break;
case 3:
return @"星期二";
break;
case 4:
return @"星期三";
break;
case 5:
return @"星期四";
break;
case 6:
return @"星期五";
break;
case 7:
return @"星期六";
break;
default:
return @"";
break;
}
}
有不懂的地方欢迎提问,时间紧没有具体写注释。
转载请注明出处,谢谢。