reloj problema posicion porque poner pantalla movil incorrecta hora fecha desconfigura como cambiar año atrasa iphone ios objective-c parse.com

problema - porque se desconfigura la hora de mi iphone



Convierta GMT NSDate en la zona horaria actual del dispositivo (6)

Crear un caso de prueba de Xcode como el siguiente puede ayudarnos a recordar las reglas "para siempre":

- (void)test2015_05_23_07_07_07_Toronto { NSString *utcDateString = @"2015_05_23 12:07:07"; NSDateFormatter *dateFormatter = [NSDateFormatter new]; dateFormatter.dateFormat = @"yyyy_MM_dd HH:mm:ss"; dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; NSDate *date2015_05_23_12_07_07_UTC = [dateFormatter dateFromString:utcDateString]; XCTAssertTrue([date2015_05_23_12_07_07_UTC.description isEqualToString:@"2015-05-23 12:07:07 +0000"]); dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"EST"]; NSString *torontoDateString = [dateFormatter stringFromDate:date2015_05_23_12_07_07_UTC]; XCTAssertTrue([torontoDateString isEqualToString:@"2015_05_23 07:07:07"]); // change format to add ZZZ dateFormatter.dateFormat = @"yyyy_MM_dd HH:mm:ss ZZZ"; torontoDateString = [dateFormatter stringFromDate:date2015_05_23_12_07_07_UTC]; XCTAssertTrue([torontoDateString isEqualToString:@"2015_05_23 07:07:07 -0500"]); XCTAssertEqual(1432382827, [date2015_05_23_12_07_07_UTC timeIntervalSince1970]); NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; calendar.timeZone = [NSTimeZone timeZoneWithName:@"EST"]; NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date2015_05_23_12_07_07_UTC]; XCTAssertEqual(2015, components.year); XCTAssertEqual(5, components.month); XCTAssertEqual(23, components.day); XCTAssertEqual(7, components.hour); XCTAssertEqual(7, components.minute); XCTAssertEqual(7, components.second); }

Estoy usando Parse.com para almacenar algunos valores:

Estos son valores GMT. ¿Cómo convierto estos a la zona horaria actual del dispositivo y obtengo NSDate como resultado?


El método más fácil que he encontrado es este:

NSDate *someDateInUTC = …; NSTimeInterval timeZoneSeconds = [[NSTimeZone localTimeZone] secondsFromGMT]; NSDate *dateInLocalTimezone = [someDateInUTC dateByAddingTimeInterval:timeZoneSeconds];



Hay muchas respuestas a esta pregunta, pero recomendaría esta:

Convierta GMT NSDate en la zona horaria actual del dispositivo

NSString *dateStr = @"2012-07-16 07:33:01"; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [dateFormatter1 dateFromString:dateStr]; NSLog(@"date : %@",date); NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone]; // <- Local time zone NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date1]; NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date1]; NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset; NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date1] autorelease]; NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init]; [dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"]; [dateFormatters setDateStyle:NSDateFormatterShortStyle]; [dateFormatters setTimeStyle:NSDateFormatterShortStyle]; [dateFormatters setDoesRelativeDateFormatting:YES]; [dateFormatters setTimeZone:[NSTimeZone systemTimeZone]]; dateStr = [dateFormatters stringFromDate: destinationDate]; NSLog(@"DateString : %@", dateStr);


NSDate siempre está representado en GMT. Es solo cómo lo representas lo que puede cambiar.

Si desea imprimir la fecha en label.text , luego label.text en una cadena utilizando NSDateFormatter y [NSTimeZone localTimeZone] , de la siguiente manera:

NSString *gmtDateString = @"08/12/2013 21:01"; NSDateFormatter *df = [NSDateFormatter new]; [df setDateFormat:@"dd/MM/yyyy HH:mm"]; //Create the date assuming the given string is in GMT df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; NSDate *date = [df dateFromString:gmtDateString]; //Create a date string in the local timezone df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT]; NSString *localDateString = [df stringFromDate:date]; NSLog(@"date = %@", localDateString); // My local timezone is: Europe/London (GMT+01:00) offset 3600 (Daylight) // prints out: date = 08/12/2013 22:01


Puede usar un NSDateFormatter para lograr este resultado.

NSString *dateAsString = @"08/07/2013 04:06"; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"dd/MM/yyyy HH:mm"]; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [df setTimeZone:gmt]; NSDate *myDate = [df dateFromString: dateAsString]; NSLog(@"date: %@", myDate);