Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a specified date is today, yesterday, or a future date

I have one query regarding NSDate. I have a date i.e. "2011-10-04 07:36:38 +0000", and I want to check if this date is yesterday, or today or a future date.

How would I go about this?

like image 219
milanjansari Avatar asked Oct 12 '11 07:10

milanjansari


1 Answers

Try this:

Note: Change the date format as per your need.

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* enteredDate = [df dateFromString:@"10/04/2011"];
NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:enteredDate];
switch (result)
{
    case NSOrderedAscending: 
        NSLog(@"Future Date");
                    break;
    case NSOrderedDescending: 
        NSLog(@"Earlier Date");
                    break;
    case NSOrderedSame: 
        NSLog(@"Today/Null Date Passed"); //Not sure why This is case when null/wrong date is passed
                    break;
}
like image 152
Shri Avatar answered Oct 14 '22 10:10

Shri