没有获得适当的时区

时间:2011-05-18 06:38:37

标签: iphone nstimezone

我是iPhone新手。我有一个问题。首先查看我的代码。

NSString *timeZone=[NSString stringWithFormat:@"%@",[[NSTimeZone systemTimeZone] abbreviation]];
NSLog(@"Print Time Zone :- '%@'",timeZone);
NSRange Timerange=[timeZone rangeOfString:@"+"];
NSString *time;

if(Timerange.location == NSNotFound)
{
    Timerange=[timeZone rangeOfString:@"-"];
    time=[timeZone substringFromIndex:Timerange.location];
    NSLog(@"Time is :- '%@'",time);
    time=[time stringByReplacingOccurrencesOfString:@"-" withString:@"M"];

}
else
{
    Timerange=[timeZone rangeOfString:@"+"];
    time=[timeZone substringFromIndex:Timerange.location];
    NSLog(@"Time is :- '%@'",time);
    time=[time stringByReplacingOccurrencesOfString:@"+" withString:@"P"];
}

它在iPhone模拟器4.2中运行正常但它在模拟器4.3中崩溃所以可能是什么问题?

由于

2 个答案:

答案 0 :(得分:1)

你可能需要像这样编码

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"z"];

NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];

答案 1 :(得分:1)

NSDate *date=[NSDate date]; 
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; 
    [formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];

    [formatter setTimeStyle:NSDateFormatterLongStyle]; 

    NSString *dateOfGame =[formatter stringFromDate:date]; 

    NSString *timeZone = [dateOfGame substringFromIndex:12];

    NSLog(@"dateOfGame%@",timeZone);

尝试使用此代码