我有NSDictionary
看起来像。
dic is : {
city = #;
country = #;
date = "2011-08-12 05:00:00 +0000";
"day_of_week" = Fri;
high = 79;
icon = "/ig/images/weather/chance_of_storm.gif";
low = 61;
startdate = "2011-08-12 05:00:00 +0000";
state = #;
}
dic is : {
city = #;
country = #;
date = "2011-08-13 05:00:00 +0000";
"day_of_week" = Fri;
high = 79;
icon = "/ig/images/weather/chance_of_storm.gif";
low = 61;
startdate = "2011-08-12 05:00:00 +0000";
state = #;
}
dic is : {
city = #;
country = #;
date = "2011-08-14 05:00:00 +0000";
"day_of_week" = Sat;
high = 79;
low = 58;
startdate = "2011-08-12 05:00:00 +0000";
state = #;
}
if(( [dd1 earlierDate:startDate]) &&[dd1 earlierDate:endDate] )
{
/*if([dd1 isEqualToDate:startDate] || [dd1 isEqualToDate:endDate] )
我正在从测试用例中传递NSDate
。从测试用例传递Startdate和enddate,并从作为日期的解析器传递dd1。我想获得2个日期之间的值,但获得所有4个日期。只获取我希望的日期值的解决方案是什么?
DD1是NSDATE对象我正在从NSDICTIONARY.SO DD1开始运行在我所在的FORCH中,我将在201-08-12,2011-08-13 ETC获取日期
答案 0 :(得分:0)
您正在尝试将NSDictionary
与NSDate
进行比较。这是您提取日期和计算差异的方法:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-mm-dd HH:mm:ss zz"];
NSDate *startDate = [formatter dateFromString:[dd1 objectForKey:@"startdate"]];
NSDate *date = [formatter dateFromString:[dd1 objectForKey:@"date"]];
NSTimeInterval intervalInSeconds = [startDate timeIntervalSinceDate:date];
[formatter release];
不是100%确定dateFormate
字符串的最后一部分,但它应该有效。
答案 1 :(得分:0)
从您发布的代码中,您所谈论的4个日期并不明显。但是,当您尝试将boolean逻辑应用于对象时,if语句没有意义(earlyDate:返回指向NSDate对象的指针)。尝试这样的事情:
if(([dd1 compare:startDate] == NSOrderedDescending) && ([dd1 compare:endDate] == NSOrderedAscending))
如果dd1表示的日期介于startDate和endDate之间,则此值应仅为true。