2012-08-25 9 views

답변

2

NSDateFormatter를 MM-dd-yyyy와 함께 사용하고 포맷터에 적용된 NSTimeZone을 사용하여 현지 시간대를 조정할 수 있습니다.

이 :

NSDate *date = [[NSDate alloc] init]; 
NSLog(@"%@", date); 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"MM-dd-yyyy hh:mm"]; 
NSString *dateString = [dateFormatter stringFromDate:date]; 

NSLog(@"%@", dateString); 

NSTimeZone *timeZone = [NSTimeZone localTimeZone]; 
[dateFormatter setTimeZone:timeZone]; 
NSLog(@"adjusted for timezone: %@", [dateFormatter stringFromDate:date]); 

출력 :

2012-08-26 08:30:53.741 Craplet[2585:707] 2012-08-26 12:30:53 +0000 
2012-08-26 08:30:53.742 Craplet[2585:707] 08-26-2012 08:30 
2012-08-26 08:30:53.743 Craplet[2585:707] adjusted for timezone: 08-26-2012 08:30 
+0

고마워요! 그것은 매력처럼 작용했습니다. 어떻게 필자가 정말로 찾고있는 것은 LOCAL dateFormat을 어떻게 든 얻는 것입니다. NSString에서 선호됩니다. 그걸 도와 줄 수있어? –

+0

현지 시간대와 함께 사용하는 방법이 업데이트되었습니다. hh : mm을 추가하여 데모 용으로 로컬임을 나타냅니다. – bryanmac

6

NSDateFormatter이 편리한 클래스 메소드가 :

[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]; 

지정할 수 있습니다 NSDateFormatterNoStyle .. ShortStyle .. MediumStyle, 또는 ..LongStyle

관련 문제