如何在iPhone目标c中显示昨天(相对于日期,但不是24小时的时间间隔)访问过的项目
我有一个带有联系人的表格视图。 因为当我选择联系人时,必须将该联系人添加到另一个表:RecentsTable 中。作为最近访问过的项目。为此,我使用了这样的代码,它可以工作,但是。 就昨天而言,它考虑的是 24 小时。但我想在日期更改时显示昨天的项目,然后自动将其添加到昨天的列表中。请指导我。
NSDate *date = self.lastviewed;
double time = [date timeIntervalSinceNow];
NSString *tmpValue = nil;
time *= -1;
if(time < 3600*24) {
tmpValue = @"Today";
}
else {
int div = round(time / 60 / 60 / 24);
// I want solution wth respect to yesterday's date date but not as follows
if(div == 1)
tmpvalue = @ "Yester Day";
if (div >1 && div <7)
//tmpValue= [NSString stringWithFormat:@"%d days ago", div];
tmpValue = @"This Week";
else if(div <30)
tmpValue = @"This Month";
else
tmpValue = @"Earlier";
}
提前致谢..
I have a table view with Contacts.
in that when i select the contact that contact has to be added in to another Table:RecentsTable. as recently visited items. for this purposs i have used code like this, it's working but.
in case of yesterday it cosidered 24 hours. but iwant to display yesterday items when ever date changed then automatically it was added in to yesterday's list. please guide me.
NSDate *date = self.lastviewed;
double time = [date timeIntervalSinceNow];
NSString *tmpValue = nil;
time *= -1;
if(time < 3600*24) {
tmpValue = @"Today";
}
else {
int div = round(time / 60 / 60 / 24);
// I want solution wth respect to yesterday's date date but not as follows
if(div == 1)
tmpvalue = @ "Yester Day";
if (div >1 && div <7)
//tmpValue= [NSString stringWithFormat:@"%d days ago", div];
tmpValue = @"This Week";
else if(div <30)
tmpValue = @"This Month";
else
tmpValue = @"Earlier";
}
thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 NSDateComponents< /a> 处理日期时。它将处理夏令时等问题。NSDate 上的 StackOverflow 上有很多问题,这个 例如,可以通过将日期部分更改为 -1 天来解决您的问题。
You should use NSDateComponents when working with dates. It will handle things like day light savings etc. There are plenty of questions on StackOverflow on NSDate, this one for example can be used to solve your problem by changing the date component to -1 day.
我认为你不仅要改变“昨天”的情况,还要改变月、年和周的其他计算。
I think you have to change not only the "Yesterday" case but also the other calculations for month, year and week.
如果您使用 NSCalendar 获取日和月单位。
请参阅此和这个一些例子。
如果您获得当天和给定的最后查看日期的日、月单位,您可以通过比较两者找到所需的值。
编辑:
It would be more easy if you use NSCalendar to get the day and month units.
Refer this and this for some examples.
If you get the day, month units for current day and the given lastViewed day, you can find the required values by comparing the both.
Edit: