how to compare two dates and time in iphone sdk

二次信任 提交于 2020-01-01 19:17:38

问题


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


回答1:


    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




回答2:


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");



回答3:


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



回答4:


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

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

}



回答5:


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

}


来源:https://stackoverflow.com/questions/13758253/how-to-compare-two-dates-and-time-in-iphone-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!