2016-07-19 1 views
0

서버에서 시간을 얻고 있습니다 2016/07/19--12:20:00 이제는이 시간을 국가에 맞게 변경해야합니다. 예를 들어 내가 오클랜드에있는 경우 시간은 같아야합니다 2016/07/19--18:50:00 스택 오버플로에서 너무 많은 솔루션을 발견했지만 해결할 수 없습니다. 나에게내 서버 시간을 다른 국가 시간으로 변환하는 방법?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"YYYY/MM/dd--HH:mm:ss"]; 
     NSDate* sourceDate = [dateFormatter dateFromString:model.tripEndTime]; 
     NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
     NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone]; 
     NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; 
     NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; 
     NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 
     NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate]; 
     NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; 
     [dateFormat setDateFormat:@"YYYY/MM/dd--HH:mm:ss"]; 
     NSString* localTime = [dateFormat stringFromDate:destinationDate]; 
     NSLog(@"localTime:%@", localTime); 

도와주세요이 코드

+0

ur 시도한 코드를 표시 할 수 있습니다 –

+0

GMT에서 서버 시간입니까? – Avi

답변

2

서버 시간을 UTC 시간으로 변환 한 다음 현지 국가 시간으로 변환하기 만하면됩니다. 이에 대한 별도의 방법을 작성하십시오.

+(NSString*)convertLocaleTimeToChatTimeStampWithDateStr:(NSString *)strDate 
{ 
NSDateFormatter *df = [DateHelper getDefaultTimeFormatter]; 
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; 
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
NSDate *date = [df dateFromString:strDate]; 
[df setDateFormat:@"dd MMM, hh:mm a"]; 
[df setTimeZone:[NSTimeZone localTimeZone]]; 
return [df stringFromDate:date]; 
} 

+(NSDateFormatter*)getDefaultTimeFormatter 
{ 
NSDateFormatter *df = [NSDateFormatter new]; 
[df setDateStyle:NSDateFormatterNoStyle]; 
[df setTimeStyle:NSDateFormatterShortStyle]; 
return df; 
} 

희망 귀하의 문제를 해결할 수 있기를 바랍니다. 감사합니다

+0

나는 직접 시간대를 설정하는 대신 전화기의 시간대를 사용하는 것이 더 좋지 않다고 생각한다. 유일한 문제는 전화가 현재 시간대로 전환하지 않은 경우입니다. – Joshua

0

서버의 타임 스탬프를 사용하는 것이 좋습니다로했습니다. 타임 스탬프를 얻은 후 시간대에 따라 변환하십시오.

+0

예제를 제공 할 수 있습니다 @ios 개발자 – Himanth

+0

NSString * timestamp = [NSString stringWithFormat : @ "% f", [[NSDate date] timeIntervalSince1970] * 1000]; –

+0

이것은 현재 타임 스탬프를 얻고 서버 타임 스탬프를 얻기 위해 서버 측 코드를 작성해야합니다. –