没有获得正确的时区

发布于 2024-11-07 22:29:48 字数 824 浏览 0 评论 0原文

我对 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 Simulator 4.2 中工作正常,但在模拟器 4.3 中崩溃,那么问题是什么?

谢谢

I am very new in iPhone. I have one issue. First Just check my code.

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"];
}

Its working proper in iPhone Simulator 4.2 but it crashes in simulator 4.3 so what can be the issue?

Thanks

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

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

发布评论

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

评论(2

吐个泡泡 2024-11-14 22:29:48
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);

尝试使用这段代码

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

try with this code

蓝色星空 2024-11-14 22:29:48

你可能需要这样编码

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

you might need coded like this

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