Recuerde que debe crear el NSDate y luego la salida se con una zona horaria y un calendario válidos
NSDate solo representa un punto absoluto en el tiempo. No tiene un concepto de zona horaria (NY, Barcelona, ...) o calendario (gregoriano, hebreo, ...).
UIDatePicker devuelve de forma predeterminada una NSDate con el sistema NSCalendar y NSTimeZone, pero cuando intenta imprimir más tarde, no da formato a la salida. Es posible que tenga allí el desajuste.
Por lo tanto, primero debe configurar el UIDatePicker correctamente y 2º transformar la salida con NSDateFormatter para conocer el calendario y la zona horaria que se está utilizando.
Un código de ejemplo, con el inicio de la UIDatePicker y luego imprimir el resultado:
- (void)viewDidLoad
{
[super viewDidLoad];
// init the UIDatePicker with your values
// by default UIDatePicker inits with today, system calendar and timezone
// Only for teaching purposes I will init with default values
NSDate * now = [[NSDate alloc] init];
[_datePicker setDate: now]
animated: YES];
_datePicker.timeZone = [NSTimeZone localTimeZone];
_datePicker.calendar = [NSCalendar currentCalendar];
[_datePicker addTarget: self
action: @selector(getDatePickerSelection:)
forControlEvents:UIControlEventValueChanged];
}
-(void)getDatePickerSelection:(id) sender
{
// NSDateFormatter automatically inits with system calendar and timezone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Setup an output style
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
// Medium style date, short style time => "Nov 23, 1937 3:30pm"
NSString *dateString = [dateFormatter stringFromDate:date];
}
Verifica el que hice para otra pregunta muy similar:
NSDate output using NSDateFormatter
¿Está utilizando un UIImagePicker (como en su pregunta) o un UIDatePicker (como en el título de su pregunta)? ¿Estás personalizando el selector de fecha de alguna manera? –
mi error, estoy usando UIDatePicker – MTA