开发者

How can I compare "today" (NSDate) to a formatted string date (dd/MM/yyyy)?

I'm new to iOS programming and I've been looking a lot for a way to compare "today" (an NSDate) to a formatted string date (dd/MM/yyyy).

I've found some good answers with the NSDate's earlierDate, laterDate etc. but the code doesn't seem to work in my project.

here it is :

// [book quandd]  is the string :    25/02/2011  (or whatever dd/MM/yyyy date)

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

if ( [[dateFormat dateFromString:[book quandd]] isEqualToDate:today]) {
    NSLog(@"Dates are equal");
}
if ( [[dateFormat dateFromString:[book quandd]] earlierDate:today]) 开发者_StackOverflow中文版{
    NSLog(@"earlier");
}
if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]) {
    NSLog(@"later");
}

Whatever I do and whatever the date in [book quandd], the console always writes "earlier" and "later", as if they were later and earlier at the same time than today.

Where is that "bug" coming from ?

Is there some easy/easier way to compare the today date with 26/02/2011 for example ?


An NSDate includes both date and time. No two NSDate values will be equal unless the times are identical down to the millisecond (if not finer). When you do [NSDate date] you get the exact time of "now", while if you use NSDateFormatter to convert a string to date (with no time value) then midnight is used.

If you want to see if two NSDates fall on the same day you can use NSCalendar & NSDateComponents. Or you can "cheat" and format both dates to a date string with no time value and compare the strings.

- (void) CompareDate:(NSDate*)date1 toDate:(NSDate*)date2 {
    NSDateFormatter* fmt = [NSDateFormatter new];
    [fmt setDateFormat:@"yyyyMMdd"];
    // Note that you should set the appropriate timezone if you don't want the default.
    NSString* date1Str = [fmt stringFromDate:date1];
    NSString* date2Str = [fmt stringFromDate:date2];
    switch ([date1Str compare:date2Str]) {
        case NSOrderedAscending:
            NSLog(@"Date 1 is earlier");
            break;
        case NSOrderedSame:
            NSLog(@"Dates are equal");
            break;
        case NSOrderedDescending:
            NSLog(@"Date 2 is earlier");
            break;
    }
}


Use timeIntervalSinceNow: to get the diff from the necessary date.

You should also try to get the difference between them and then convert to a string.


use this

NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];

    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedSame) {
        NSLog(@"Dates are equal");
    }
    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedAscending) {
        NSLog(@"earlier");
    }
    if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]==NSOrderedDescending) {
        NSLog(@"later");
    }


You need to check return value of the earlierDate with one of the dates referred. Ex: if([date1 earlierDate:date2] == date1)


Your "bug" happens because laterDate and earlierDate return the date object that is either earlier or later. The return object is always nonnil, so both if statements evaluate to true. The correct way to use them is this way:

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date laterDate:today] == today ) {
    NSLog(@"earlier");
}
if ( [date laterDate:today] == date) {
    NSLog(@"later");
}

Alternatively, I've used timeIntervalSinceDate, which returns the seconds between the first date and the second date.

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date timeIntervalSinceDate:today] < 0 ) {
    NSLog(@"earlier");
}
if ( [date timeIntervalSinceDate:today] > 0) {
    NSLog(@"later");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜