2012-08-04 5 views
1

IOS에서 GMT를 IST로 변환하려면 어떻게해야합니까? 나는 그리니치 표준시로 시간을 줄 코드가있다. 하지만 해당 지역 시간대로 변환하여 내 앱에서 사용하고 싶습니다. 어떻게해야합니까? 미리 감사드립니다.iOS에서 GMT를 IST로 변환

답변

8

아래 코드는 GMT를 IST로 변환합니다.

NSString *inDateStr = @"2000/01/02 03:04:05"; 
NSString *s = @"yyyy/MM/dd HH:mm:ss"; 

// about input date(GMT) 
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init]; 
inDateFormatter.dateFormat = s; 
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
NSDate *inDate = [inDateFormatter dateFromString:inDateStr]; 

// about output date(IST) 
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init]; 
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"]; 
outDateFormatter.dateFormat = s; 
NSString *outDateStr = [outDateFormatter stringFromDate:inDate]; 

// final output 
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr); 
1
[(NSDateFormatter *) setTimeZone:[NSTimeZone localTimeZone]]; this will give u the local conversion of the GMT to local time zone. 
1

GMT를 IST로 변환하기 위해 아래 코드를 사용했습니다.

NSTimeZone *zone1=[NSTimeZone localTimeZone]; 
long second=[zone1 secondsFromGMT]; //offset 
NSDate *currentdate=[NSDate date]; 

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"MMddyyyy hh:mm:ss"]; // format 
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:second]]; 

dateStr = [dateFormat stringFromDate:currentdate]; 
NSLog(@"Current date %@",dateStr); 

유용 할 수도 있습니다.

감사합니다.

관련 문제