Reputation: 2284
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..
Upvotes: 3
Views: 2702
Reputation: 3359
I think you have to change not only the "Yesterday" case but also the other calculations for month, year and week.
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";
Upvotes: 2
Reputation: 17478
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:
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";
}
Upvotes: 0
Reputation: 56645
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.
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];
Upvotes: 4