简体   繁体   中英

how to compare two dates and time in iphone sdk

here is what I am trying

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *serDate;
NSDate *endDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
serDate = [formatter dateFromString:@"2012-12-07 7:17:58"];
endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];
int remainigSecond =[endDate timeIntervalSinceDate:serDate];
NSString * Hrs = @"1.30";// is Hrs
if (remainigSecond>3600*[Hrs intValue])
    return 1;
else
    return 0;

it does not response expected... Can you help he whats wrong in this please

Thanks in Advance

compare: function can be used to compare two dates effectively.

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *serDate;
NSDate *endDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
serDate = [formatter dateFromString:@"2012-12-07 7:17:58"];
endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];

if([serDate compare:endDate])
    NSLog(@"Enddate bigger than serdate");
else
    NSLog(@"serdate bigger than enddate");
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    NSDate *serDate;

    NSDate *endDate;

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    serDate = [formatter dateFromString:@"2012-12-07 7:17:58"];

    endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];
    NSTimeInterval timeDifference = [endDate timeIntervalSinceDate:serDate];

    double minutes = timeDifference / 60;

    double hours = minutes / 60;

    double seconds = timeDifference;

    double days = minutes / 1440;
    NSLog(@" days = %.0f,hours = %.2f, minutes = %.0f,seconds = %.0f", days, hours, minutes, seconds);

and compare like bellow..

if (hours > 1.30)
    return 1;
else
    return 0;

and the outPut is days = 0, hours = 1.43, minutes = 86,seconds = 5132

You can use this method to get YEAR , MONTH ana DAY etc between two dates

 NSCalendar *sysCalendar = [NSCalendar currentCalendar];
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

    NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:firstDate  toDate:secondDate  options:0];

    NSLog(@"Conversion: %dmin %dhours %ddays %dmoths %dYear",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]%12,[conversionInfo month]/12);
    NSString *result2 = [NSString stringWithFormat:@"%d year, %d month and %d day",[conversionInfo month]/12,[conversionInfo month]%12,[conversionInfo day]];

timeIntervalSinceReferenceDate function works very good in date comparision on two date object.

if(([endDate timeIntervalSinceReferenceDate] - [startDate timeIntervalSinceReferenceDate]) > 3600*[Hrs intValue]){

}
if(([endDate timeIntervalSinceReferenceDate] - [startDate timeIntervalSinceReferenceDate]) > 3600*[Hrs intValue])
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM