如何在iphone sdk中比较两个日期和时间 [英] how to compare two dates and time in iphone sdk
本文介绍了如何在iphone sdk中比较两个日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我正在尝试的事情
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
提前致谢
推荐答案
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);
并比较如下..
if (hours > 1.30)
return 1;
else
return 0;
且outPut 天= 0,小时= 1.43,分钟= 86 ,秒= 5132
这篇关于如何在iphone sdk中比较两个日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文