2009-05-10 5 views
5

나는 확실히 뭔가를 놓친 것 같습니다. 하지만 NSDateFormatterShortStyle 스타일로 NSDate를 표시 할 때 4 자리 연도를 표시하는 방법을 찾으려고 며칠 동안 이것을 봤습니다. 해결책이 있다면 알려주십시오. 여기에 지금 사용하고있는 코드가 있습니다.아이폰 4 자리 연도로 shortdate

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; 
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; 

    // add label for birth 
    UILabel *birthday = [[UILabel alloc] initWithFrame:CGRectMake(125, 10, 100, 25)]; 
    birthday.textAlignment = UITextAlignmentRight; 
    birthday.tag = kLabelTag; 
    birthday.font = [UIFont boldSystemFontOfSize:14]; 
    birthday.textColor = [UIColor grayColor]; 
    birthday.text = [dateFormatter stringFromDate:p.Birth]; 

답변

10

네 자리 연도가 필요한 경우 정확한 형식을 사용하여 dateformat:을 설정하십시오. 단점은 자동 로케일 서식을 잃게된다는 것입니다. 당신은 현지화가 필요한 경우

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setDateFormat:@"MM/dd/yyyy"]; 

... 

birthday.text = [dateFormatter stringFromDate:p.Birth]; 

당신은 짧은 스타일의 날짜 형식을 수정 시도 할 수 있습니다.

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ]; 
[dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
[dateFormatter setTimeStyle:NSDateFormatterNoStyle]; 

if ([[dateFormatter dateFormat] rangeOfString:@"yyyy"].location == NSNotFound) { 
    NSString *fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString:@"yy" withString:@"yyyy"]; 
    [dateFormatter setDateFormat:fourDigitYearFormat];    
} 

맥스 마크 셀로드에서 버그 수정과 업데이트 내가 지역화 된 날짜 형식으로 2 자리 연도에 그런 일을 필요 왜냐하면 내가 '했고,이 같이 누군가가 여기에 비틀 거리고 것을 그냥 경우

+0

감사하지만 로컬 화하려면 – Dave

+0

을 현지화해야합니다. dateFormatter.locale = [NSLocale autoupdatingCurrentLocale]; –

+0

실제로 로케일 전용 형식이 이미 yyyy 인 경우를 대비하여 확인이 필요합니다. 만약 NSString * fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString : @ "yy"withString : @ "yyyy"]; [dateFormatter setDateFormat : fourDigitYearFormat]; } –

0

는 kCFDateFormatterShortStyle 는 "11/23/37"또는 "오후 3시 반"으로 만 일반적으로 숫자, 짧은 스타일을 지정합니다.

Mac OS X v10.3 이상에서 사용할 수 있습니다.

CFDateFormatter.h에 선언되어 있습니다.

Source

NSDateFormatterShortStyle는 4 자리 연도를 제공하기 위해 표시되지 않습니다.

+3

이 종류이다 : 광고,하지만 난 데이브가 원하고 할 수 없습니다 어디에 올바른 주어진 대답 것도, 여기에 솔루션 없기 때문에 일을하려고했기 때문에 (경우 누군가에 이제까지 원하는 것은 같은 일이다) 내 요점. 4 자리 연도를 제공하는 월/일/년 형식이 없습니다. 형식으로 넣으면 지역화가 없어집니다. 이것은 나의 좌절감이다. – Dave

+1

사실 그것은 로캘에 따라 다릅니다. 덴마크는 짧은 형식으로 4 자리 연도를 제공합니다. 그것은 ':'을 사용하지 않기 때문에 훌륭한 테스트 케이스입니다. –

10

NSLocale*   curLocale = [NSLocale currentLocale]; 
NSString*   dateFmt = [NSDateFormatter dateFormatFromTemplate: @"ddMMyyyy" 
                   options: 0 
                   locale: curLocale]; 
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 
        [formatter setDateFormat:dateFmt]; 
NSString*   retText = [formatter stringFromDate:refDate]; 

return retText; 

어쩌면 누군가가 언젠가 그것을 사용할 수 있습니다 ... 이것은 오래된 THRE입니다

+0

환상적! 나는 항상 다른 여러 가지 솔루션으로 어려움을 겪었으며 핵심 부분을 강타했습니다. 고마워, 친구. 좋은 일을 계속하십시오. – Felipe

1

: 정말 오래된 질문은 여기 내 솔루션입니다

NSString *FormatString = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy HH:mm" options:0 locale:[NSLocale currentLocale]]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:FormatString]; 
//Parse date here with the formater (like [formatter stringFromDate:date]) 
[formatter release]; 
관련 문제