如何在iPhone objective c中显示昨天(相对于日期,而不是24小时的时间间隔)访问的项目
我有一个带有联系人的表视图。 在这种情况下,当我选择联系人时,该联系人必须添加到另一个表中:RecentsTable。最近访问的项目。为了这个目的,我使用了这样的代码,它可以工作,但是。 就昨天而言,它是24小时的。但我想在日期发生变化时显示昨天的项目,然后自动将其添加到昨天的列表中。请引导我如何在iPhone objective c中显示昨天(相对于日期,而不是24小时的时间间隔)访问的项目,iphone,objective-c,xcode,nsdate,nsdateformatter,Iphone,Objective C,Xcode,Nsdate,Nsdateformatter,我有一个带有联系人的表视图。 在这种情况下,当我选择联系人时,该联系人必须添加到另一个表中:RecentsTable。最近访问的项目。为了这个目的,我使用了这样的代码,它可以工作,但是。 就昨天而言,它是24小时的。但我想在日期发生变化时显示昨天的项目,然后自动将其添加到昨天的列表中。请引导我 NSDate *date = self.lastviewed; double time = [date timeIntervalSinceNow];
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";
}
NSDate*date=self.lastviewed;
双时间=[date-TimeIntervalenceNow];
NSString*tmpValue=nil;
时间*=-1;
如果(时间<3600*24){
tmpValue=@“今天”;
}
否则{
int div=四舍五入(时间/60/60/24);
//我要的是关于昨天日期的解决方案,但不是如下所示
如果(div==1)
tmpvalue=@“昨天”;
if(div>1&&div处理日期时应使用。它将处理日光节约等问题。例如,NSDate上存在大量有关StackOverflow的问题,可以通过将日期组件更改为-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];
如果你用它来计算日和月的单位会更容易
有关一些示例,请参阅和
如果获得了当前日期和给定的上次查看日期的日、月单位,则可以通过比较两者来找到所需的值
编辑:
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";
}
NSDate*todate=[NSDate-date];
NSDate*lastViewedDate=self.lastviewed;
NSCalendar*格里高利=[[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags=NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents*components=[gregorian components:unitFlags
fromDate:lastViewedDate
toDate:toDate选项:0];
NSInteger月=[组件月];
NSInteger天数=[组件日];
NSString*tmpValue=nil;
如果(天数==0){
tmpValue=@“今天”;
}
其他的
{
如果(天数==1)
tmpValue=@“昨天”;
否则,如果(天>1&&days我认为您不仅必须更改“昨天”的情况,还必须更改月、年和周的其他计算
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";
NSDate*date=self.lastviewed;
//双时间=[date-TimeIntervalenceNow];
NSCalendar*currentCalendar=[NSCalendar currentCalendar];
NSDateComponents*componentsOfDate=[currentCalendar
组件:(NSYearCalendarUnit | NSMonthCalendarUnit)|
NSDayCalendarUnit | NSWeekCalendarUnit)
fromDate:日期];
NSDateComponents*componentsOfToday=[currentCalendar
组件:(NSYearCalendarUnit | NSMonthCalendarUnit)|
NSDayCalendarUnit | NSWeekCalendarUnit)
fromDate:[NSDate日期]];
如果([ComponentSofDate year]==[ComponentSofDay year])
如果([componentsOfDate月]==[ComponentSofDay月])
如果([componentsOfDate]===[ComponentSofDay])
tmpvalue=@“今天”;
其他的
tmpvalue=@“本月”;
其他的
tmpvalue=@“今年”;
//本周计算,简单的方法。
如果([componentsOfDate week]==[ComponentSofDay week]&&
abs([date-timeIntervalSinceNow])首先,阅读并搜索NSDateComponents和NSCalendar。如果您仍然不确定或只是想了解更多信息,请观看WWDC的11次iOS日期和日历编程会话。所有答案都在这里。