1
내 응용 프로그램은 GMT +1 (UTC/GMT +1 시간) 시간대 인 원격 서버에서 날짜/시간을 가져옵니다. 서버가 제공사용자의 현지 시간으로 날짜/시간 변환 - 문제
형식은 다음과 같습니다
24 08 2011 오후 8시 45분
나는 사용자의 시간대에 해당하는 날짜/시간으로이 시간 스탬프를 변환하고 싶습니다
(사용자는 전 세계 어디에나있을 수 있습니다.) 서버에서 오는 24 08 2011 08:45 오후 이탈리아 사용자 (로마) (GMT + 1)이을에
24 08 2011 오후 9시 45분을 제시한다 : 예를 들어, 따라서
코드는 일부 시간대에 작동하지만 난 그것에 대해 매우 문제가있는 것으로 나쁜 느낌이하고NSString *dateString = @"24 08 2011 09:45PM"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd MM yyyy hh:mma"]; NSDate *dateFromString = [[[NSDate alloc] init] autorelease]; dateFromString = [dateFormatter dateFromString:dateString]; NSDate* sourceDate = dateFromString; NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"BST"]; NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ; NSString *thePubDate = [dateFormatter stringFromDate:destinationDate];//[appLogic getPubDate]; NSLog(@"Result : %@",thePubDate); [dateFormatter release]; //[dateFromString release]; [destinationDate release];
내가 문제
에 대한 의견과 제안을 주셔서 감사합니다 할 수있는 훨씬 더 우아한 방법이 있음
위의 코드는 모두 작동하지만 두 변수의 이름이 중복되고 일부 변수 이름이 일치하지 않으면 더 명확 해집니다. –