-1

I have a fairly working app which just requires a few tweaks and some assistance to get there.

The premise is simple: there is a two-tabbed table view controller and when the user presses the + button in the navigation bar, they're presented with text fields to fill in and a date picker for the date of the event. They press save and it saves to Core Data and then therefore displays that information in the table view. The table view cells contain names and titles, but the section contains the selected dates. The second tab of the table view displays (or should display) only the dates, allowing the user to select which date they want to see the events for.

My "Dates" were recently NSStrings with textFields, before changing that to NSDate and UIDatePicker. *The problem right now is the fact that the section title of the table view is displaying the raw date from the DatePicker, which includes something like 2013-02-01 13:01 0000+. What I want is to change that format to use the NSDateFormatterLong style so it's December 2, 2013 without any time, etc. *

I have worked through a few examples of this type of thing on SO but nothing seems to be working for me.

The model is as follows: - Transaction Entity - Date Entity - Person Entity - Occasion Entity

The Transaction has a relationship with all other entities, including Date (transaction.dateOfEvent).

When I save to Core data for the date, I am calling the method below:

- (IBAction)save:(id)sender
{
    NSManagedObjectContext *context = [self managedObjectContext];
    Transaction *transaction = [NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:context];
    Date *date = [NSEntityDescription insertNewObjectForEntityForName:@"Date" inManagedObjectContext:context];
    date.dateOfEvent = self.datePicker.date;
    transaction.dates = date;
}

That works fine and the table view (using NSFetchedResultsControllers) works really well with the chronological dates.

What I want is to change the format of the string from 2013/02/01, etc to December 2, 2013 and I want this same format to be used within the cells of the second tab table view - I'm not sure how to convert that NSDate to an identifiable string for both the section title and the cell textLabel

Any assistance on this would be massively appreciated. Thanks,

EDIT: Including titleForSection method in the TableView

// Following http://stackoverflow.com/questions/4418703/a-nsfetchedresultscontroller-with-date-as-sectionnamekeypath.. I have tried the following

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  NSString *rawDateStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
  // Convert rawDateStr string to NSDate...
  NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
  [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZ"];
  NSDate *date = [formatter dateFromString:rawDateStr];

  // Convert NSDate to format we want...
  [formatter setDateFormat:@"d MMMM yyyy"];
  NSString *formattedDateStr = [formatter stringFromDate:date];
  return formattedDateStr;  
}

// I have also tried:
NSString * sectionInfo = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSDate *date = [dateFormatter dateFromString:sectionInfo];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"%@", formattedDate);
return formattedDate;

But both are returning the exact same format (and also weirdly hiding most of the section titles leaving just a bit of visibility) but they are showing the exact same format.

amitsbajaj
  • 1,304
  • 1
  • 24
  • 59
  • There are literally thousands of examples of using NSDateFormatter here. Do a little research. Start by reading the spec for NSDateFormatter, especially the paragraphs at the top, and maybe follow some of the links there. – Hot Licks Dec 02 '13 at 13:09
  • Use `NSDateFormatter` to create a string in the format you want to display. – zaph Dec 02 '13 at 13:09
  • And show us what you have tried. – Hot Licks Dec 02 '13 at 13:11
  • Thanks @HotLicks - I have updated my question to include attempted code - thanks – amitsbajaj Dec 02 '13 at 13:17
  • What format are they returning, and what did you want? – Hot Licks Dec 02 '13 at 16:31
  • @HotLicks Thanks for the reply - I've answered the question and managed to find the answer - thanks for you looking into this! – amitsbajaj Dec 02 '13 at 17:07
  • Understand that NSDate has no format -- all the formatting is done when converting to string, and the particulars of that format are determined by the format string. – Hot Licks Dec 02 '13 at 18:12

2 Answers2

0
NSString myStr = @"2013-12-03 09:22:45";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";     
NSDate *myDT = [dateFormatter dateFromString:myStr];
dateFormatter.dateFormat = @"dd-MMM-yyyy";
NSLog(@"%@",[dateFormatter stringFromDate:myDT]);
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • Thanks @Vizlix - the question I have is how do I use NSFetchedResultsController to obtain the value for myStr - would I just use something like: NSString * myStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name];? Also, would that give me the December 3, 2013 format? – amitsbajaj Dec 02 '13 at 13:19
  • @amitsbajaj- Why not mate! 1. if "NSString * myStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name];" this contains proper date then it will format the date. 2. if you want it in this format "December 3, 2013" then change dateFormatter.dateFormat = @"dd-MMM-yyyy"; to @"MMM-dd-yyyy" – Vizllx Dec 02 '13 at 13:33
  • Thanks @Vizllx - I just tried that and the NSLog is returning NULL along with the date in the section remaining the same as it was - I put NSString *myStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name]; right before your code and returned myStr at the end of this titleForHeader - thanks for your help on this - I feel we're close.. I just can't figure out why it's returning NULL - though the selected date from the datePicker is appearing in the section title which is getting that from Core Data, so I suspect it something we're almost doing? – amitsbajaj Dec 02 '13 at 14:31
  • It seems that from this question (http://stackoverflow.com/questions/4418703/a-nsfetchedresultscontroller-with-date-as-sectionnamekeypath), even if we somehow get the format working.. it will create a new section even if the date is the same for 2 events - so if I have december 3, 2013 and two separate entries, that'll create two separate sections with the same.. which is not what I want - I need to of course get the formatting right and then I can see if that is the case – amitsbajaj Dec 02 '13 at 15:24
0

I have managed to find a fix for this issue and thought it would be important to paste the answer here.

Based on this question here: Core Data sort tableview by formatted date, I created a simple property in the Transaction Entity and made the sectionName to be that property.

So, just to be clear for any onlookers to this question. I have a Transaction Entity which has a relationship with the Person, Occasion and Date Entities. I created the following in the header of the Transaction NSManagedObject Subclass:

@property (nonatomic, readonly) NSString *sectionDateFormatter;
-(NSString *)sectionDateFormatter
{
    return [NSDateFormatter localizedStringFromDate:self.dates.dateOfEvent
                                          dateStyle:NSDateFormatterLongStyle
                                          timeStyle:NSDateFormatterNoStyle];
}

In the table view NSFetchedResultsController, my NSSortDescriptor was the date, but the sectionNameKeyPath was that created property:

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"sectionDateFormatter" cacheName:nil];

If you want to take this a step further and provide the same code for table view cells (not just sections), copy the NSDateFormatter localisedStringFromDate: code above and assign it to a NSString:

Date *date = [self.fetchedResultsController objectAtIndexPath:indexPath];

NSString *stringDate [NSDateFormatter localizedStringFromDate:self.dates.dateOfEvent
                                      dateStyle:NSDateFormatterLongStyle
                                      timeStyle:NSDateFormatterNoStyle];

customCell.datesLabel.text = stringDate; 

Thanks all for your help!

Community
  • 1
  • 1
amitsbajaj
  • 1,304
  • 1
  • 24
  • 59