2014-01-06 3 views
0

날짜에서 요일 번호를 가져오고 싶습니다. 내가 dayNum을 부탁 dayNum요일 숫자 "dd"에 대한 날짜 형식 지정

38의 대신 (7)를 개최 얻을 the_dte2014-01-07 경우, 예를 들어

NSDateFormatter* numDay = [[MSDateFormatter alloc]init]; 
numDay.dateFormat = @"DD"; 
dateString = [numDay stringFromDate:the_dte]; 
DayNum.text = [NSString stringWithFormat:@"%@", dateString]; 

: 나는 바람직하지 않은 출력을 생산하고 다음과 같은 코드가 있습니다. 내 형식과 관련이 있다고 가정합니다. 나는 내가 원하는 것을 얻기 위해 그것을 넣을 형식을 모른다.

+1

은 [문서를 참조하십시오 레이블 텍스트에 그 할당 할 수 있습니다 ] (http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns). 달의 경우 "d"또는 "dd"를 사용하십시오. "DD"는 일년 내내 당신을 얻게 될 것입니다. 요일을 잡기위한 포맷터 코드가 없습니다. NSCalendarComponents를 사용해야합니다. –

+0

와우. 그건 아주 간단했습니다. 감사. –

+0

해당 사이트를 북마크 해두면 시간을 절약 할 수 있습니다. –

답변

0

예에서 the_dte은 NSDate 여야합니다. 그렇다면 this answer to get exactly what you are looking for을 읽어야합니다.

은 기본적으로 당신은 그럼 당신은있는 NSString의 stringWithFormat:을 사용하려는 어떤 문자열에 해당 구성 요소의 형식을

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:the_dte]; 
NSInteger day = [components day]; 
NSInteger month = [components month]; 
NSInteger year = [components year]; 

하여있는 NSDate에서 날짜 구성 요소를 얻을