2013-02-12 13 views
5
は、私のような文字列に私の日付と時刻の書式を次のコードしている

iOSの日付書式の日付書式を追加する方法「目」、「ST」

2013年10月25日(月曜日) - 午後2時56分34秒

NSDate *today = [[NSDate alloc] init]; 
dateFormatter = [[NSDateFormatter alloc] init]; 
[self.dateFormatter setDateFormat:@"EEEE, MMMM dd YYYY - HH:mm:ss"]; 

NSString *currentTime = [self.dateFormatter stringFromDate: today]; 
self.timerLabel.text = currentTime.uppercaseString; 

は、それはうまく動作します。しかし、どのように "th"か "rd"か "st"を上記の例の25の後の日付に適切に配置するにはどうすればよいですか?

答えて

5
NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
[format setDateFormat:@"MMMM dd"]; 

NSDate *now = [[NSDate alloc] init]; 

NSString *dateString = [format stringFromDate:[now dateByAddingTimeInterval:(-60*60*24*10)]]; 
NSMutableString *tempDate = [[NSMutableString alloc]initWithString:dateString]; 
int day = [[tempDate substringFromIndex:[tempDate length]-2] intValue]; 
switch (day) { 
    case 1: 
    **case 21: 
    case 31:** 
     [tempDate appendString:@"st"]; 
     break; 
    case 2: 
    **case 22:** 
     [tempDate appendString:@"nd"]; 
     break; 
    case 3: 
    **case 23:** 
     [tempDate appendString:@"rd"]; 
     break; 
    default: 
     [tempDate appendString:@"th"]; 
     break; 
} 
NSLog(@"%@",tempDate); 
+10

ローカライズされた方法はありますか? – keegan3d