我有一个带有联系人的表格视图。 当我选择联系人必须添加到另一个表: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";
}
提前感谢..
答案 0 :(得分:4)
使用日期时,您应该使用NSDateComponents。它将处理日光节省等问题。在NSDate上有很多关于StackOverflow的问题,例如this one可以通过将日期组件更改为-1天来解决您的问题。
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSDate *yesterday = [gregorian dateByAddingComponents:dayComponent
toDate:today
options:0];
答案 1 :(得分:2)
我认为您不仅要更改“昨天”案例,还要更改月,年和周的其他计算。
NSDate *date = self.lastviewed;
// double time = [date timeIntervalSinceNow];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents * componentsOfDate = [currentCalendar
components: (NSYearCalendarUnit|NSMonthCalendarUnit|
NSDayCalendarUnit|NSWeekCalendarUnit)
fromDate:date];
NSDateComponents * componentsOfToday = [currentCalendar
components:(NSYearCalendarUnit|NSMonthCalendarUnit|
NSDayCalendarUnit|NSWeekCalendarUnit)
fromDate:[NSDate date]];
if ([componensOfDate year] == [componentsOfToday year])
if ([componentsOfDate month] == [componentsOfToday month])
if([componentsOfDate day] == [componentsOfToday day])
tmpvalue = @"Today" ;
else
tmpvalue = @"This Month" ;
else
tmpvalue = @"This Year" ;
// This week calculation, the easy way.
if([componentsOfDate week] == [componentsOfToday week] &&
abs([date timeIntervalSinceNow]) <= (7*24*60*60))
tmpvalue = @"This Week" ;
// Yesterday calculation
NSDate * yesterday = [NSDate dateWithTimeInterval:(-24*7*60*60) sinceDate:[NSDate date]];
NSComponents *componentsOfYesterday = [currentCalendar
components: (NSYearCalendarUnit|NSMonthCalendarUnit|
NSDayCalendarUnit)
fromDate:yesterday];
// check this, as I'm not sure you can use compare: on components object
if([componentsOfDate compare:componentsOfYesterday] == NSOrderedSame)
tmpvalue = @"Yesterday";
答案 2 :(得分:0)
如果您使用NSCalendar获取日期和月份单位会更容易。
如果您获得当天和给定的lastViewed日的日,月单位,您可以通过比较两者来找到所需的值。
修改强>
NSDate *todate = [NSDate date];
NSDate *lastViewedDate = self.lastviewed;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:lastViewedDate
toDate:todate options:0];
NSInteger months = [components month];
NSInteger days = [components day];
NSString *tmpValue = nil;
if ( days == 0 ) {
tmpValue = @"Today";
}
else
{
if(days == 1)
tmpValue = @"Yesterday";
else if (days >1 && days <7)
//tmpValue= [NSString stringWithFormat:@"%d days ago", div];
tmpValue = @"This Week";
else if(days <30)
tmpValue = @"This Month";
else
tmpValue = @"Earlier";
}