在Instagram程序中捕捉,日期显示为(例如)2小时前/ 3天前/ 1周前..我有“2013-03-20 16:02:48”。 。我怎样才能得到字符串“2天前”?NSDate返回小时/天/周
回答
考虑使用NSCalendar和NSDateComponents来计算差异并清晰地表达与最终用户类似的内容。
有一个开源项目
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是tr ansformed为“在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."
好的。但请注意,其许可证需要归属。 – Toastor 2013-03-22 12:16:45
不是,如果你只是查找一些实现细节。无论如何:在信贷到期时给予信用... – vikingosegundo 2013-03-22 12:17:44
我不在意归属。相反,我的意见是提醒尊重许可证,因为归属是最容易“忘记”的要求之一。 – Toastor 2013-03-22 12:21:50
也许有什么地方出库,这将做到这一点你。
但是不管怎样都不难。有可能将NSDate分解为其组件。然后很容易比较它们,并编写一些如果其他代码覆盖所有需要的情况。
下面是从我的项目之一,我解决过去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天,因为我在代码中以不同的方式覆盖了这些情况。但我认为它会给你一个如何做到这一点的基本想法。
- 1. 的NSDate返回错误的周数
- 2. NSDate formatString有时返回无
- 3. 从参考NSDate获取一周的第二天的NSDate?
- 4. 返回周末天的MySQL函数
- 5. NSDate -dateFromString返回nil
- 6. 的NSDate返回零
- 7. 的NSDate返回null
- 8. NSDate dateFromString返回NULL
- 9. NSDate dateFromString返回零
- 10. (日历)天小时不返回正确的小时
- 11. 今天得到NSDate,昨天,本周,上周,本月,上个月...变量
- 12. NSDate日期比它应该返回2小时
- 13. NSDate额外返回5小时30分钟
- 14. SQLite组by/count小时,天,周,年
- 15. nginx HttpLimitReqModule支持每小时/天/周吗?
- 16. NSDate天数倒计时
- 17. NSDate dateWithTimeIntervalSince1970不返回GMT/UTC时间
- 18. 从NSDate获取时间返回零
- 19. 发送消息15周一天24小时时间每小时
- 20. NSDate早1小时?
- 21. 当一天不等于24小时时向NSDate添加一天 - iOS
- 22. NSDate在第一天返回错误的日期
- 23. 返回的NSDate不正确
- 24. NSDate stringFromDate返回空错误?
- 25. UIDatePicker + NSDate返回空值
- 26. Nsdate总是返回空
- 27. Nsdate转换返回空
- 28. 的NSDate格式返回null?
- 29. 强制NSDate返回24小时时间而不管区域设置
- 30. NSString到NSDate为“15分钟”或“1小时”或“1天”
这个问题不是xcode相关的,因为它没关系,如果代码是用Xcode,vi编写的或者是用电报输入的。请仅在需要了解xcode编辑器本身的情况下使用Xcode标签。 – vikingosegundo 2013-03-22 12:03:24
NSDateComponents对象,不适用于iOS?我用这个对象解决了这个问题。所以不要聪明 – LightNight 2013-03-25 10:24:19
你在说什么? – vikingosegundo 2013-03-25 12:26:05