Вопрос

Я очень новый в 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, но он вылетает в Simulator 4.3, так что может быть проблемой?

Спасибо

Это было полезно?

Решение

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

Другие советы

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];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top