NSDateFormatter和时区问题? [英] NSDateFormatter and Time Zone issue?

查看:124
本文介绍了NSDateFormatter和时区问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GMT中有一个时间字符串,我想根据系统时区制作但不能正常工作 -

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]);

控制台输出 -

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

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

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

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

系统时区=亚洲/加尔各答(IST)抵消19800

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

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

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

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

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

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

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

加尔各答格林威治标准时间+5:30,所以日期的时间应为1 :27但显示02:27。此外,当我从这个日期采取一个字符串,它向我显示我曾经用于生成日期的相同刺痛,我希望根据系统时区更新字符串。

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.

谢谢

推荐答案

如果日期字符串是GMT,你可以不要使用您的系统Timezone从NSString创建NSDate。

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

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

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

这篇关于NSDateFormatter和时区问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆