简体   繁体   English

如何在iphone sdk中比较两个日期和时间

[英]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. compare:function可用于有效地比较两个日期。

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 outPut是days = 0, hours = 1.43, minutes = 86,seconds = 5132

You can use this method to get YEAR , MONTH ana DAY etc between two dates 您可以使用此方法在两个日期之间获得YEARMONTH和 DAY

 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. timeIntervalSinceReferenceDate函数在两个日期对象的日期比较中工作得非常好。

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

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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