Instagram程序中的快照,日期显示为(例如)2小时前/3天前/1周前..我有“2013-03-20 16:02:48”..我怎样才能得到字符串“2天前“?
问问题
558 次
3 回答
1
考虑使用NSCalendar和NSDateComponents来计算差异并与最终用户相似。
于 2013-03-22T11:51:52.697 回答
1
有一个开源项目
https://github.com/billgarrison/SORelativeDateTransformer
从其文档中:
例如,当前日期时间为 2010-12-05 11:30,则
... 2010-12-05 11:00 转换为“30 分钟前”
... 2010-12-01 11:00转换为“5天前”
... 2010-12-25 08:00 转换为“2周内”// Display a file's creation date relative to today's date NSURL *someFilePath = ...; NSDictionary *attribs = [[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:NULL]; NSDate *dateModified = [attribs fileModificationDate]; // fileModificationDate is 2010-10-01 12:00:00TZ; // Date now is 2010-10-30 12:00:00TZ SORelativeDateTransformer *relativeDateTransformer = [[SORelativeDateTransformer alloc] init]; NSString *relativeDate = [relativeDateTransformer transformedValue: dateModified]; NSLog (@"This file was modified %@.", relativeDate); // ==> "This file was modified 3 weeks ago."
于 2013-03-22T12:13:41.477 回答
0
也许某处有一个图书馆可以为您做到这一点。
但无论如何,这并不难。可以将 NSDate 分解为其组件。然后很容易比较它们并编写一些 if then else 代码来涵盖所有需要的情况。
这是我的一个项目中的一个示例,我在过去 7 天内将日期解析为适当的工作日:
NSString *resultDateString;
// First, reduce the two dates we want to compare to the scope of days by erasing hours, minutes and seconds
unsigned int flagA = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *compsA = [[NSCalendar currentCalendar] components:flagA fromDate:myObjectsDate];
[compsA setHour:0];
[compsA setMinute:0];
[compsA setSecond:0];
NSDate *checkDate = [[NSCalendar currentCalendar] dateFromComponents:compsA];
unsigned int flagB = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *partsB = [[NSCalendar currentCalendar] components:flagB fromDate:[NSDate date]];
[compsB setHour:0];
[compsB setMinute:0];
[compsB setSecond:0];
NSDate *nowDate = [[NSCalendar currentCalendar] dateFromComponents:compsB];
// Get the interval between the two dates, interval is still in seconds, but since it is calculated using days only, it will be precisely 0 if the day is the same
NSTimeInterval interval = [searchDate timeIntervalSinceDate:nowDate];
// check if the objects date is today. If it isn't, find out which day it is and name it appropriately
if (interval == 0) {
resultDateString = [NSString stringWithString:@"Today"];
} else {
unsigned int flagC = NSWeekdayCalendarUnit;
NSDateComponents *compsC = [[NSCalendar currentCalendar] components:flagC fromDate:myObjectsDate];
switch ([compsC weekday]) {
case 1:
resultDateString = @"Sunday";
break;
case 2:
resultDateString = @"Monday";
break;
case 3:
resultDateString = @"Tuesday";
break;
case 4:
resultDateString = @"Wednesday";
break;
case 5:
resultDateString = @"Thursday";
break;
case 6:
resultDateString = @"Friday";
break;
case 7:
resultDateString = @"Saturday";
break;
default:
break;
}
}
这只是一个摘录,请注意,此代码片段不会检查间隔是否长于 7 天,因为我在代码中以不同的方式涵盖了此类情况。但我认为它会给你一个如何做到这一点的基本概念。
于 2013-03-22T12:14:02.987 回答