0

Alright, here is the skinny. I've searched on this but haven't found this question quite right. (I'm writing this in xcode fyi)

I have been tasked with something that should be easy and it probably is but I'm having trouble getting started...

I need to download JSON data and it is a list of objects that only have three items, date, tite, contents. Basically 3 strings. The date is not the date created but the date the whole object is intended for. The users are wanting to be able to preload a few weeks at a time w/o people being able to see the future dates.

I can download the JSON data no problem, I can display it in a list, not a problem. I am not certain how to make the program ignore objects with a date not equal to today.

Anyone have any insight into this? Below is what one of the test objects looks like.

@type: "Devotional",
@url: "http://api.storageroomapp.com/accounts/50ff0ad80f66027d84001680/collections/51c1ba270f6602499f0000ae/entries/51c1bc390f6602442c0002ec",
@collection_url: "http://api.storageroomapp.com/accounts/50ff0ad80f66027d84001680/collections/51c1ba270f6602499f0000ae",
@version: 2,
@trash: false,
@created_at: "2013-06-19T14:12:09Z",
@updated_at: "2013-07-08T19:38:51Z",
devotional_date: "2013-07-08",
devotional_title: "Test Today Devotional",
devotional_body: "This is a test of what Today's Daily Devotional would look like!"
},

Thanks in advance for any help.

ZachC
  • 1
  • 3
  • The problem has more to do with the language you are using to process the data than with JSON. If you tag your question appropriately (with the language), you will be getting more views. – Felix Kling Jul 08 '13 at 20:03
  • Where the dates come from doesn't matter. The actual problem is comparing dates. You need to build a new list of relevant items by copying the items from the old list only if the dates match todays date. For comparing dates, see http://stackoverflow.com/questions/1811779/comparing-dates-in-coc – Paul Jul 08 '13 at 20:34
  • This has absolutely nothing to do with JSON. – Hot Licks Jul 08 '13 at 23:01
  • The data I'm going to be comparing with is originating in JSON. – ZachC Jul 11 '13 at 22:18

1 Answers1

0

Here is the basic idea for doing this comparison:

- (void)testDateIsToday {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now = [NSDate date];
    NSInteger dateUnits = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDateComponents *dateParts = [gregorian components:dateUnits fromDate:now];
    NSString *dateString = @"2013-07-08"; // Date from your JSON
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSDate *requestedDate = [dateFormatter dateFromString:dateString];
    NSDateComponents *requestedParts = [gregorian components:dateUnits fromDate:requestedDate];

    NSLog(@"Equal? %i", [[gregorian dateFromComponents:requestedParts] compare:[gregorian dateFromComponents:dateParts]] == NSOrderedSame);
}

Note that this will do the comparison based on the time zone the device it runs on is set in, which I'm guessing will not impact you since you don't specify time zone in your JSON. Using a calendar and date components is the safest way to do such a comparison.

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
  • See also: http://stackoverflow.com/questions/5331512/comparing-certain-components-of-nsdate – Ben Flynn Jul 09 '13 at 00:05
  • Awesome, thanks for the guidance on that. Wasn't quite sure where to start on that piece. I'll give this a shot in the next couple of days and come back with what I got. Thank you very much for the help. – ZachC Jul 11 '13 at 22:17