[英]Convert NSString to NSDate with UTC-5 timezone formatter
I have this NSDateFormatter
: 我有这个
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:
NSString *geolocaliser=@"09/11/2014 03:03:20 UTC+1";
and I want to convert it to an NSDate object: 我想将其转换为NSDate对象:
NSDate *lastLocatedUserDate =[formatter dateFromString:geolocaliser];
The output here is perfect: lastLocatedUserDate=2014-11-09 02:03:20 +0000
这里的输出是完美的:
lastLocatedUserDate=2014-11-09 02:03:20 +0000
Now if I have this NSString: 现在,如果我有这个NSString:
NSString *geolocaliser=@"11/11/2014 12:07:40 UTC−5" ;
I'm getting a null value : lastLocatedUserDate=(null)
我得到一个空值:
lastLocatedUserDate=(null)
I'm thinking that the problem is because UTC-5
, but it's not the same thing as UTC+1
? 我以为问题是因为
UTC-5
,但这和UTC+1
不是同一回事吗?
Thank you for helping . 感谢您的帮助 。
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. 您的字符串
@"11/11/2014 12:07:40 UTC−5"
中包含无效字符。 The apparent dash in the timezone is actually the Unicode character "MINUS SIGN" (U+2212) instead of a plain old "HYPHEN-MINUS" (U+002D). 时区中的明显破折号实际上是Unicode字符“ MINUS SIGN”(U + 2212),而不是普通的旧“ HYPHEN-MINUS”(U + 002D)。
Replace the −
with -
and your code will work properly. 更换
−
与-
和你的代码将正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.