简体   繁体   中英

Converting NSString to NSDate with different timezones, for use with a UIDatePicker

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM