3

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..

iOS dev
  • 2,254
  • 6
  • 33
  • 56
  • 2
    First, read http://developer.apple.com/library/ios/ipad/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html and then search for NSDateComponents and NSCalendar. If you're still unsure or just want more info, watch the WWDC'11 session on date and calendar programming for iOS. All your answers are there. – Jason Coco Jan 10 '12 at 05:49

3 Answers3

4

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];
Community
  • 1
  • 1
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
2

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";
Gabriel
  • 3,319
  • 1
  • 16
  • 21
0

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";
}
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55