NSDateFormatter 和时区问题?

发布于 2024-12-20 08:57:25 字数 1108 浏览 2 评论 0原文

我有一个带有 GMT 时间的字符串,我想根据系统时区来制作它,但它无法正常工作 -

NSLog(@"Time Str = %@",Time);

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy hh:mm a"];

[NSTimeZone resetSystemTimeZone];

NSLog(@"system time zone = %@",[NSTimeZone systemTimeZone]);

[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

NSDate *date = [dateFormat dateFromString:Time];

[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"date from string = %@",date);
NSLog(@"string from date = %@",[dateFormat stringFromDate:date]);

控制台输出 -

/////////////////////////////////////////////////////////// ///////////////////////////////////////////////

时间 Str = 09 -12-2011 07:57 AM

系统时间区域 = 亚洲/加尔各答 (IST) 偏移量 19800

字符串日期 = 2011-12-09 02:27:00 +0000

字符串日期 = 09-12-2011 07:57 AM

/////////////////////////////////////////////////////////// ///////////////////////////////////////////////

加尔各答 +5 :30(格林尼治标准时间),所以日期时间应该是1:27 但显示的是 02:27。另外,当我从该日期获取一个字符串时,它向我显示与我用来制作日期的相同的字符串,我希望根据系统时区更新该字符串。

谢谢

I have a string with time in GMT and i want to make it according to the system time zone but its not working properly -

NSLog(@"Time Str = %@",Time);

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy hh:mm a"];

[NSTimeZone resetSystemTimeZone];

NSLog(@"system time zone = %@",[NSTimeZone systemTimeZone]);

[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

NSDate *date = [dateFormat dateFromString:Time];

[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"date from string = %@",date);
NSLog(@"string from date = %@",[dateFormat stringFromDate:date]);

Output at console -

////////////////////////////////////////////////////////////////////////////////////////////////

Time Str = 09-12-2011 07:57 AM

system time zone = Asia/Calcutta (IST) offset 19800

date from string = 2011-12-09 02:27:00 +0000

string from date = 09-12-2011 07:57 AM

////////////////////////////////////////////////////////////////////////////////////////////////

Calcutta is +5:30 from GMT, so the time in date should be 1:27 but its showing 02:27. Also when i take a string from this date its showing me the same sting that i used to make date, i want the string updated according system time zone.

Thanks

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

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

发布评论

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

评论(1

动次打次papapa 2024-12-27 08:57:25

如果日期字符串采用 GMT,则无法使用系统时区从 NSString 创建 NSDate。

将第一次出现的 [dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; 替换

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

If the date string is in GMT you can't use your system Timezone to create the NSDate from the NSString.

replace the first occurrence of [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

with [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

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