NSdate 假设时区错误

发布于 2025-01-08 16:14:39 字数 507 浏览 3 评论 0原文

我正在尝试将以下字符串转换为 NSDate 对象:

NSString *str=@"25 May 2012 10:25:00";

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"asia/kolkata"]];
 NSDate *date = [dateFormatter dateFromString:str];


In console : date-->2012-05-25 04:55:00 +0000....it lags behind 5 hours and 30 minutes and assumes GMT timezone instead of Asia...Why it is so?

I am trying to convert the following string into an NSDate object:

NSString *str=@"25 May 2012 10:25:00";

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"asia/kolkata"]];
 NSDate *date = [dateFormatter dateFromString:str];


In console : date-->2012-05-25 04:55:00 +0000....it lags behind 5 hours and 30 minutes and assumes GMT timezone instead of Asia...Why it is so?

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

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

发布评论

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

评论(2

恰似旧人归 2025-01-15 16:14:39

当您在控制台中看到打印的[NSDate描述]时,它始终是GMT中相应的时间。如果您使用相同的日期格式化程序将日期转换回字符串,则它应该位于指定的时区。

[NSDate 描述] 是您键入

po date

po [date description]

使用 NSLog 将这些表单之一发送到控制台时看到的内容。

When you see an [NSDate description] printed in the console, it is always the corresponding time in GMT. If you use the same date formatter to convert the date back to a string, it should be in the specified time zone.

An [NSDate description] is what you see if you type

po date

or

po [date description]

or you use NSLog to send either one of these forms to the console.

枕头说它不想醒 2025-01-15 16:14:39

如果您正在寻找印度时区,您应该使用:
在此处输入图像描述

if you are looking for India Timezone you should use:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文