Não Recebendo O Fuso Horário Adequado
-
14-11-2019 - |
Pergunta
Eu sou muito novo no iPhone.Eu tenho um problema.Primeiro Basta verificar o meu código.
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"];
}
Seu trabalho adequado no iPhone Simulator 4.2, mas ele falha em simulador 4.3 então, o que pode ser o problema?
Obrigado
Solução
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);
tente com esse código
Outras dicas
você pode precisar codificada como este
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];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow