2012-02-16 5 views
-1

Here은 objective-c에서 날짜를 사용하는 것에 대한 이전 질문입니다. 0을 앞에두고시, 분, 초를 얻는 방법?앞에 오는 0으로 시간을 받으십시오.

+0

예제를 제공해 줄 수 있습니까? 그들을 문자열이나 정수로 가져 오시겠습니까? – sch

+0

여기를 참조하십시오 http://stackoverflow.com/questions/9317320/getting-current-time – OhDoh

+0

왜 내 질문에 투표가 내려 졌습니까? – OhDoh

답변

5

은 그들을 얻는 방법이다 :

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; 

NSInteger hour= [components hour]; 
NSInteger minute = [components minute]; 
NSInteger second = [components second]; 

NSString *hourString = [NSString stringWithFormat:@"%02d", hour]; 
NSString *minuteString = [NSString stringWithFormat:@"%02d", minute]; 
NSString *secondString = [NSString stringWithFormat:@"%02d", second]; 

의 핵심은 포맷 할 때 당신이 얻을 것을 보장하기 위해, "%02d"을 사용하는 것입니다 두 자릿수.

+0

감사합니다! 그 트릭을 했어! – OhDoh

2

"날짜"를 문자열로 지정하고 문자열에 0을 앞에두고 싶다고 생각합니다. 여기 당신이 문자열로 시간, 분, 초를 원하는 precised 이제

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"HH:mm:ss"]; 
NSString *formattedDateString = [dateFormatter stringFromDate:date]; 
+0

고맙지 만 시간, 분, 초를 코드에 별도로 액세스하고 싶습니다. – OhDoh

+0

예. ** NSDate * date = [NSDate date]; ** – sch

관련 문제