0

I am looking to convert a NSString to an NSDate and would just like to ask a few questions about this.

The purpose of the conversion is to take a section header in a table view which is a string derived from an NSDate, and to pass it back to a UIDatePicker, so that the date picker can use that date in the string.

I've been reading some posts about this and there are tons of formats to work through.

My string date format is in the form:

March 10, 2014 for American formats and 10 March 2014 for UK formats.

What would I need to do to:

1) Get that date to translate over to the UIDatePicker appropriately 2) Work with different country formats so that the date picker is updated for UK and US users.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"d-MMMM-YYYY"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:sectionTitle];

dateFromString is currently null.

I've been looking at https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html , Converting NSString to NSDate (and back again) and http://waracle.net/iphone-nsdateformatter-date-formatting-table/ but am not sure how to proceed.

Any assistance would be appreciated on this

Community
  • 1
  • 1
amitsbajaj
  • 1,304
  • 1
  • 24
  • 59
  • Why are you passing the date as a string? Why not keep track of the original `NSDate` used to generate the section header and pass that instead? – rmaddy Mar 11 '14 at 17:40
  • have a look at NSCalendar... – Armand DOHM Mar 11 '14 at 17:43
  • "d-MMMM-YYYY" is a bogus format. You almost certainly do not want the upper-case "YYYY". – Hot Licks Mar 11 '14 at 17:54
  • @rmaddy - I had to go through a long process to ensure the dates are represented in a clean format as section titles which is why I had to pass the NSString there.. it just wouldn't be right any other way in my case here.. so now I just need to pass that string "back" to the UIDatePicker.. – amitsbajaj Mar 11 '14 at 17:55
  • To get the date picker to use the correct timezone and locale, set its timezone and locale. You don't separately format the date, but pass in an NSDate object (which is timezone/locale agnostic). – Hot Licks Mar 11 '14 at 17:57
  • (Keep the original date as an attribute of the table view section header, vs trying to pull the date out of the header.) – Hot Licks Mar 11 '14 at 18:00

1 Answers1

0

I know this isn't directly answering the question, but it makes several of the above comments make more sense:

How to create the data source structure for a table view:

NSMutableArray* sections = [NSMutableArray array];
for (<each section>) {
    NSMutableDictionary* section = [NSMutableDictionary dictionary];
    [section setObject:sectionTitle forKey:@"title"];
    [section setObject:sectionNSDate forKey:@"date"];
    [section setObject:otherStuff forKey:@"other_stuff"];
    NSMutableArray* rows = [NSMutableArray array];
    for (<each row>) {
        NSMutableDictionary* row = [NSMutableDictionary dictionary];
        [row setObject:rowTitle forKey:@"title"];
        [row setObject:image forKey:@"image"];
        [rows addObject:row];
    }
    [section addObject:rows forKey:@"rows"];
    [sections addObject:section];
}

It's easy to keep track of & update section and row data, the number of sections and number of rows methods virtually write themselves, you don't need to re-derive data if something is scrolled away and comes back (since you can cache it in the dictionary).

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Many thanks for this - this is really useful. Because I have too many factors in the app dependent on the way it's working now, I'm going to keep this information handy for an update because there'd be too much to change. It does however make complete sense and I really appreciate you putting this together. If I were to format the NSDate in the UIDatePicker to a specific format, say American for example, firstly, how would I do that? Also, could I then convert from the string to the date (sorry, I know it's not what you're saying is the best option).. and if so, what format would I use? – amitsbajaj Mar 11 '14 at 18:22
  • @Lavanya - To format the date in the date picker you set the date picker attributes -- locale, timezone, sometimes calendar (if the calendar isn't Gregorian). Remember, the NSDate object itself contains absolutely no timezone or locale or format info -- it's only a number. – Hot Licks Mar 11 '14 at 18:35