iPhone - NSDate、NSString 和时区的奇怪问题
我已经搜索过,但没有找到像我这样的确切问题,所以这里什么也没有:
我有一个 NSString,其中包含我从 XML feed 中提取的密钥。关键是 24 小时格式的时间(例如 13:30 或 15:00)。我想将 NSString 转换为 NSDate,并根据设备设置的时区将其转换为适当的时区。关键是 Unicode HH:mm (24:00),所以我很好奇为什么这不能正常工作。
我已经有了一个应该可行的基本大纲,但可惜没有。第二个 NSLog (获取 NS 日期)返回 null,最终日志返回一个奇怪的数字(准确地说是 1969-12--2147483629 -596:-31:-23 +0000。)我在这里做错了什么?
提前致谢,
NSString *dateString = [dict objectForKey:@"24hrdate"];
NSLog(@"NSString Date: %@", dateString);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSDate *sourceDate = [formatter dateFromString:dateString];
NSLog(@"Got NS Date: %@", sourceDate);
NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
NSLog(@"Final Date: %@", destinationDate);
I've searched and haven't found an exact question like mine, so here goes nothing:
I have a NSString containing a key that I pull from an XML feed. The key is a time in 24-hour format (e.g. 13:30 or 15:00.) I'd like to convert the NSString to an NSDate and have it converted to the appropriate timezone based on the device's set timezone. The key is Unicode HH:mm (24:00), so I'm curious why this does not work as it should.
I've already gotten a basic outline that should work, but alas does not. The 2nd NSLog (Got NS Date) returns null and the final log returns a strange number (1969-12--2147483629 -596:-31:-23 +0000 to be precise.) What am I doing wrong here?
Thanks in advance,
NSString *dateString = [dict objectForKey:@"24hrdate"];
NSLog(@"NSString Date: %@", dateString);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSDate *sourceDate = [formatter dateFromString:dateString];
NSLog(@"Got NS Date: %@", sourceDate);
NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
NSLog(@"Final Date: %@", destinationDate);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,请了解日期部分将为
01-01-1970
,因为未提供它。我假设如果输入字符串为@"04:00"
,您希望时间为04:00 GMT
。您可以通过设置格式化程序的时区来实现。First of all understand that the date component will be
01-01-1970
because it isn't provided. I am assuming that you want the time to be04:00 GMT
if the input string is@"04:00"
. That you can achieve by setting the time zone of the formatter.NSDate
用于表示日期和时间。虽然您可以通过坚持午夜来仅表示日期,但您不能真正用它来表示一天中的某个时间。你可以在 Mac 上伪造这个(默认为某个合理的日期),但在 iOS 上你会得到非常不准确的时间。 (至少,您在某些版本上这样做。这可能已被修复。)这里有两种方法:
您可以根据一天中的时间并使用
dateByAddingComponents
构建NSDateComponents
code> 将其添加到您希望时间显示的日期的午夜。这将无法返回您期望的夏令时开始或结束日期的时间。您可以使用所需的日期 (
NSDate
) 和时间(可能作为NSString
)构建日期/时间字符串。如果您真的只想要一天中的时间,只需将其作为字符串保留即可。
NSDate
is used to represent a date and time. While you can represent just a date by sticking with midnight, you can't really represent just a time of day with it. You can sort of fake this on the Mac (it defaults to some reasonable day), but on iOS you'll get wildly inaccurate times instead. (At least, you do on certain versions. This may have been fixed.)There's two approaches here:
You can build a
NSDateComponents
from your time of day and usingdateByAddingComponents
to add that to midnight on the date you want the time to appear on. This will fail to return the time you expect on a day where daylight savings begins or ends.You can build a date/time string using the date you want (
NSDate
) and the time (likely, as aNSString
).If you really want just the time of day, just keep it around as a string.
只是想回复一下,我确实成功地实现了我最初设定的目标,没有任何问题。基本上,我必须将字符串转换为 NSDate,通过 NSDateFormatter 运行该 NSDate(设置为时间的原始时区 - NSDateFormatter 的 setTimeZone 很有帮助),从中提取 NSDate,然后通过设备时区的另一个 NSDateFormatter 运行它。然后我将生成的 NSDate 转换回 NSString,并将其粘贴在 UILabel 上。
这个解决方案似乎工作得很好,因为我已将我的设备设置为不同的时区,并且时区更改仍然正确。
编辑:这也很重要:
Just wanted to post back that I did manage to achieve what I initially set out to do without any issues. Basically, I had to convert the string to NSDate, run that NSDate through a NSDateFormatter (set to the time's original timezone--NSDateFormatter's setTimeZone was helpful), pull an NSDate out of that, and then run that through another NSDateFormatter for the device's timezone. I then converted the resulting NSDate back to NSString, and stuck it on a UILabel.
This solution seems to have worked quite well, as I've set my devices to various timezones, and the timezone change is still correct.
EDIT: this was important to included, too: