Select particular dates from nsdictionary
I have an NSDictionary
which looks like.
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] )
I am passing NSDate
s from test cases. Startdate and enddate are passed from the test cases and dd1 is passed from the parser which is the date. I am trying to get values between 2 dates but getting all 4 dates. What is the solution to get only the date values I wish?
DD1 IS NSDATE OBJECT AND I AM GETTING IT FROM NSDICTIONARY.SO DD1 IS RUNNING INSIDE A FORLOOP WHERE I AM GETTING DATES AS 201-08-12,2011-08-13 ETC
You are trying to compare a NSDictionary
to an NSDate
. This is how you would extract the dates and calculate the difference:
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];
Not 100% sure about the last part of the dateFormate
string, but it should work.
From the bit of code that you did post, it's not apparent what 4 dates you are talking about. However, your if statement doesn't make sense as you are trying to apply boolean logic to objects (earlierDate: returns a pointer to an NSDate object). Try something like this:
if(([dd1 compare:startDate] == NSOrderedDescending) && ([dd1 compare:endDate] == NSOrderedAscending))
This should only evaluate to true if the date that dd1 represents is between startDate and endDate.
精彩评论