简体   繁体   中英

NSDateFormatter: I Don't want the time!

I am playing around with the coredatabooks source code example from the apple website, or here . I am trying to set the books copyright date attribute value to replace the author as the tableview section header, and I need the date value to be static , meaning I don't need the time, otherwise all of the date values are different, and no two books objects with the same month day and year copyright date lineup under the same date because the time portion of the date value is changing...

Here is the code from my RootViewController.m file that formats the datepicker date value for display in the section header:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *rawDateStr = [[[fetchedResultsController sections] objectAtIndex:section] name];

//convert default date 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:@"EEEE MMMM d"];
NSString *formattedDateStr = [formatter stringFromDate:date];

return formattedDateStr;

}

That outputs just fine, but it isnt letting me group books with the same copyright month day and year in the same section because again (I am assuming) the date value has the time as well. The actual saving of the date value from the datepicker is done in the EditingViewController.m file as follows:

 - (IBAction)save {

// Pass current value to the edited object, then pop.
if (editingDate) {
    [editedObject setValue:datePicker.date forKey:editedFieldKey];
}

The save method currently takes the date as-is from the datepicker, so how do I modify that code to first copy the datepicker's date to a local variable and blank out the time in there and then use that value in the setValue statement so it groups correctly? PLEASE HELP! Thanks

Since date's inherently include time, you're going to have to modify the NSDate object itself by converting it into its NSDateComponent s, blanking out the HH:mm:ss , and then converting those date components back into an NSDate.

For example:

NSDate * aDate = [NSDate date];
NSDateComponents * components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:aDate];
NSDate * convertedDate = [[NSCalendar currentCalendar] dateFromComponents:components];

If you do this, you'll lose things like timezone offset, so you might want to consider the side effects of that.

The nice thing about this method is that it works regardless of how the string is formatted (which is a fragile thing to rely on).

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