开发者

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?


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;
}


See Apple's documentation on date calculations:

NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:startDate
                                          toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

If days is between +1 and -1 then your date is a candidate for being "today". Obviously you'll need to think about how you handle hours. Presumably the easiest thing would be to set all dates to be 00:00.00 hours on the day in question (truncate the date using an approach like this), and then use those values for the calculation. That way you'd get 0 for today, -1 for yesterday, +1 for tomorrow, and any other value would likewise tell you how far things were in the future or the past.


Use any of the folowing according to ur need,

– earlierDate:
– laterDate:
– compare:

Refer this http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html


-(NSString*)timeAgoFor:(NSString*)tipping_date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:tipping_date];
    NSString *key = @"";
    NSTimeInterval ti = [date timeIntervalSinceDate:[NSDate date]];
    key = (ti > 0) ? @"Left" : @"Ago";

    ti = ABS(ti);
    NSDate * today = [NSDate date];
    NSComparisonResult result = [today compare:date];

    if (result == NSOrderedSame) {
        return[NSString stringWithFormat:@"Today"];
    }
    else if (ti < 86400 * 2) {
        return[NSString stringWithFormat:@"1 Day %@",key];
    }else if (ti < 86400 * 7) {
        int diff = round(ti / 60 / 60 / 24);
        return[NSString stringWithFormat:@"%d Days %@", diff,key];
    }else {
        int diff = round(ti / (86400 * 7));
        return[NSString stringWithFormat:@"%d Wks %@", diff,key];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜