2013-04-03 11 views
0

장치의 현재 (로컬) 날짜 및 시간으로 날짜 및 시간을 변환하는 중에 문제가 발생했습니다. API에서 2013-04-03 01:07:56 +0000 형식으로 날짜를 가져 오는 중입니다.이 날짜를 현지 날짜 및 시간 형식으로 변환하고 표시하려고합니다. 아무도 내가 어떻게 이것을 얻을 수 있는지 제안 해 주시겠습니까?날짜 및 시간을 현지 날짜 및 시간 형식으로 변환

답변

2

그냥 dateFormatter에서 시간대를 설정,이 코드는 충분하다

NSString *dateString = @"24 08 2011 09:45PM"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"]; 
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"BST"]; 
[dateFormatter setTimeZone:sourceTimeZone]; 
NSDate *dateFromString = [dateFormatter dateFromString:dateString]; 

dateFromString 이제 날짜가 24 08 2011 08:45시 (GMT) .. 그럼 현지 시간 문자열이 변환해야합니다 다음 코드를 작성하십시오.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[formatter setTimeZone:[NSTimeZone localTimeZone]]; 
[formatter setLocale:[NSLocale systemLocale]]; 

[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"]; 
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString]; 
+0

감사합니다. Kirti,하지만 디바이스 형식에 따라 날짜 형식을 표시하고 싶습니다. 여기 내 애플 리케이션은 또한 현지화를 지원합니다. – Vijay

+0

@Vijay이 URl을 따르십시오. http://stackoverflow.com/questions/1081647/how-to-convert-time-to-the-timezone-of-the-iphone-device –

+0

제안 해 주셔서 감사합니다. +1 – Vijay

관련 문제