Question

i am trying to compare two dates one from the database and the other one is the current date of the system .the date from the database is parsed into a string and i want to transform it into NSDate . the problem that im facing is that NewsDate is nil.

  -(void)viewDidLoad
    {

    // Do any additional setup after loading the view.
     self.title=self.ntitle;
     _lblDate.text=self.ndateNews;
     _lblDetail.text=self.ndetails;
     _lblTitre.text=self.title;
     NSDate *currDate=[NSDate date];
     NSDateFormatter *DateFormats=[[NSDateFormatter alloc ]init];
     [DateFormats setDateFormat:@"MM-DD-yyyy hh:mm:ss +EEEE"];
     NSLog(@"selected date  %@",self.ndateNews);
     NSDate *NewsDate=[DateFormats dateFromString:self.ndateNews];
     NSLog(@"string transformed   into date %@",NewsDate);
     NSLog(@" system date %@",currDate);



     if ([currDate isEqualToDate:NewsDate]){

        _lblDate.text=@"today";

    }

}
Was it helpful?

Solution

You are using the wrong date format pattern see this page for info.

yyyy-MM-dd'T'HH:mm:ssZZZZZ

Is what you want for your date format.

Also if you want to check that NewsDate is today you should use:

BOOL today = [[NSCalendar currentCalendar] isDateInToday:NewsDate];

Checking if a date is equal to NSDate date would me that the dates would have to matches exactly - down to the millisecond!

Note that -isDateInToday: is iOS 7 only. Leave me a comment if you want a version that works for iOS 6 too :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top