Bug in stringFromDate


#1

hello!!!

this is my first thread

Im having an issue with a pickerview which displays a date on a textfield, I formatted date with stringFromDate and everything works fine
but when I try to set date to December 30th 2012 on textfield changes it automatically to December 30th 2013

gonna share my code and several screenshots

thanks in advance for your support

Juan

#import "StartEndEventVC.h"


@interface StartEndEventVC ()

@end

@implementation StartEndEventVC
@synthesize datePicker,
            SwitchDate,
            fecFinDate,
            fecInicioDate;



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Datepicker initial settings
    datePicker.timeZone = [NSTimeZone localTimeZone];
    datePicker.locale   = [NSLocale currentLocale];
    datePicker.calendar = [NSCalendar currentCalendar];
    
    //Date format initial settings
    df             = [NSDateFormatter new];
    [df setDateFormat:@"EE, dd MMM YYYY HH:mm a"];
    [df setTimeZone:[NSTimeZone localTimeZone]];

    //initial cell to interact with datepicker
    selectedCell    = @"startDate";
}

-(void)viewDidAppear:(BOOL)animated{
    
    if (!((fecInicioDate == nil) || (fecFinDate == nil)) ) {
        self.startDateLabel.text    = [df stringFromDate:fecInicioDate];
        self.endDateLabel.text      = [df stringFromDate:fecFinDate];
    }
}


-(IBAction)adjustDate:(id)sender{
    if ([selectedCell isEqualToString:@"startDate"]){

        fecInicioDate = [datePicker date];
        [self comparaFechaInicio:fecInicioDate fecFinal:fecFinDate];
    }
    else
        if ([selectedCell isEqualToString:@"endDate"]){
            
            fecFinDate = [datePicker date];
            self.endDateLabel.text  = [df stringFromDate:datePicker.date];
        }
}

-(IBAction)changeDateType:(id)sender{
    if (self.SwitchDate.on){
        
        self.datePicker.datePickerMode  = UIDatePickerModeDate;
        [df setDateFormat:@"EE, dd MMM YYYY"];
        }
    else{
        self.datePicker.datePickerMode  = UIDatePickerModeDateAndTime;
        [df setDateFormat: @"EE, dd MMM YYYY HH:mm a"];
    }
    [self comparaFechaInicio:fecInicioDate fecFinal:fecFinDate];
}


-(void)comparaFechaInicio:(NSDate *)fecInicial fecFinal:(NSDate *)fecFinal{
     
    if ([fecInicial timeIntervalSinceDate] >= 0) {
        if (self.SwitchDate.on)     fecFinal = [fecInicial dateByAddingTimeInterval:60*60*24];
        else
        if(!(self.SwitchDate.on))   fecFinal = [fecInicial dateByAddingTimeInterval:60*60];
    }
    
    self.startDateLabel.text    = [df stringFromDate:fecInicial];
    self.endDateLabel.text      = [df stringFromDate];
    
    NSLog(@"Hardcoded date: %@", fecInicial);
    NSLog(@"formatted date: %@", [df stringFromDate:fecInicial]);
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"doneStartEnd"]) {
        [segue.destinationViewController setFechaInicioVisitaElement:self.startDateLabel.text];
        [segue.destinationViewController setFechaFinVisitaElement:self.endDateLabel.text];
    }
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            selectedCell = @"startDate";
            datePicker.date = fecInicioDate;
            
            break;
        case 1:
            selectedCell = @"endDate";
            datePicker.date = fecFinDate;
            default:
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end

Screenshots:

everything works fine here:

when I change date to December 30th 2012

same thing with Date-only option