Problema de NSDate con la configuración regional

I'm working on an iPad app that speaks to a private PHP API that I built up to communicate to a database. This API has to transfer Three dates, a starting time, an end time and the current time.

I want to test if the current time is in-between the date range. So I took the times (like 16:50:00) and convert them into NSDates:

NSDateFormatter *former = [[NSDateformatter alloc] init];
[former setDateFormat:@"HH:mm:ss"];
[former setLocale:[NSLocale currentLocale]];

NSDate *fromDate = [former dateFromString:@"10:15:00"];
NSDate *nowDate = [former dateFromString:@"13:25:00"];
NSDate *toDate = [former dateFromString:@"16:30:00"];

When I'm now logging the dates by using NSLog(@"\n%@\n%@\n%@", fromDate, nowDate, toDate); I see that the locale is incorrect.

It seems as the NSDateFormatter just ignores the locale setting. I also logged the currentLocales identifier which is the right one ("de_DE" in my case"). The locale remains +0000.

How can I fix this issue? Thanks, with kind regards, Julian

preguntado el 12 de junio de 12 a las 16:06

1 Respuestas

Alright, I've removed the previous answer, try the following

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *zone = [NSTimeZone localTimeZone];
[formatter setTimeZone:zone];
[formatter setDateFormat:@"HH:mm:ss"];

NSDate *date = [formatter dateFromString:@"10:15:00"];

NSLog(@"Time %@",[formatter stringFromDate:date]);

Respondido el 07 de diciembre de 12 a las 04:12

Hey, thanks! The date got the wrong time, though. It seems like NSDate can't have the right locale. Because it remains +0000. Strange behavior, but a solid workaround. Thanks! - Julián F. Weinert

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.