Convert NSString to NSDate with UTC-5 timezone formatter

649 views Asked by At

I have this NSDateFormatter:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"dd/MM/yyyy HH:mm:ss zzz"];

I have this NSString :

NSString *geolocaliser=@"09/11/2014 03:03:20 UTC+1";

and I want to convert it to an NSDate object:

NSDate *lastLocatedUserDate =[formatter dateFromString:geolocaliser];

The output here is perfect: lastLocatedUserDate=2014-11-09 02:03:20 +0000

Now if I have this NSString:

NSString *geolocaliser=@"11/11/2014 12:07:40 UTC−5" ;

I'm getting a null value : lastLocatedUserDate=(null)

I'm thinking that the problem is because UTC-5, but it's not the same thing as UTC+1?

Thank you for helping .

1

There are 1 answers

3
rmaddy On BEST ANSWER

It turns out to be a very non-obvious issue.

Your string @"11/11/2014 12:07:40 UTC−5" has an invalid character in it. The apparent dash in the timezone is actually the Unicode character "MINUS SIGN" (U+2212) instead of a plain old "HYPHEN-MINUS" (U+002D).

Replace the with - and your code will work properly.