根据时区创建 NSDate 对象

发布于 2024-11-25 02:16:48 字数 621 浏览 2 评论 0原文

我从我的应用程序中获得以下日期格式。

07/18/2011/04/45/EDT

我将其子串为“07/18/2011/04/45”和“EDT”。

使用以下代码我编写了一个日期。

   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"MM/dd/yyyy/HH/mm"];
   [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EDT"]];

   NSDate *currentServerDate = [dateFormatter dateFromString:formattedDateString];
   NSLog(@"currentServerDate %@",currentServerDate);

但是,当我打印 NSDate (使用上面的格式化程序创建)时,它打印为“2011-07-18 14:15:00 +0530”,其中 +0530 是我的时区。我需要编写相对于 EDT 时区的时间。我该怎么做?

谢谢

I get the following date format from my application.

07/18/2011/04/45/EDT

I sub-string that to "07/18/2011/04/45" and "EDT".

Using the following code I compose a date.

   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"MM/dd/yyyy/HH/mm"];
   [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EDT"]];

   NSDate *currentServerDate = [dateFormatter dateFromString:formattedDateString];
   NSLog(@"currentServerDate %@",currentServerDate);

However when I print the NSDate (created using above formattter) it print as "2011-07-18 14:15:00 +0530" where +0530 is my timezone. I need to compose the relative to the EDT timezone. How do I do that?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-12-02 02:16:49

我认为时区可能不是你想象的那样。尝试记录 timeZoneWithAbbreviation: 看看它告诉你什么。另外,请查看我链接到的文档,其中显示:

一般来说,除了“UTC”或“GMT”等独特实例之外,不鼓励您使用缩写。时区缩写没有标准化,因此给定的缩写可能有多种含义 - 例如,“EST”指美国和澳大利亚的东部时间

I think the time zone is probably not what you think it is. Try logging the time zone returned by timeZoneWithAbbreviation: and seeing what it tells you. Also, review the documentation I linked to where it says:

In general, you are discouraged from using abbreviations except for unique instances such as “UTC” or “GMT”. Time Zone abbreviations are not standardized and so a given abbreviation may have multiple meanings—for example, “EST” refers to Eastern Time in both the United States and Australia

信愁 2024-12-02 02:16:49

默认情况下,NSDatedescription 方法显示 GTM 中的日期以及系统的区域设置偏移量。如果您希望使用 NSLog 来生成 NSDate,请使用以下其中一项:

NSLog(@"date = %@", [date descriptionWithLocale:some_other_locale])
NSDateFormatter *formatter = ...
NSLog(@"date = %@", [formatter stringFromDate:date]);

By default NSDate's description method shows date in GTM with your system's locale offset. If you wish to pring NSDate using NSLog use one of following:

NSLog(@"date = %@", [date descriptionWithLocale:some_other_locale])
NSDateFormatter *formatter = ...
NSLog(@"date = %@", [formatter stringFromDate:date]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文