Question

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

Était-ce utile?

La solution

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]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top