Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中添加迄今为止的顺序缩写格式,例如;",&引用;";_Ios_Date_Nsdate - Fatal编程技术网

如何在iOS中添加迄今为止的顺序缩写格式,例如;",&引用;";

如何在iOS中添加迄今为止的顺序缩写格式,例如;",&引用;";,ios,date,nsdate,Ios,Date,Nsdate,我有以下代码将我的日期和时间格式化为字符串,如下所示: 2013年10月25日星期一-14:56:34 NSDate *today = [[NSDate alloc] init]; dateFormatter = [[NSDateFormatter alloc] init]; [self.dateFormatter setDateFormat:@"EEEE, MMMM dd YYYY - HH:mm:ss"]; NSString *currentTime = [self.dateFormatt

我有以下代码将我的日期和时间格式化为字符串,如下所示:

2013年10月25日星期一-14: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之后?

是否有一种本地化方法可以做到这一点?这很简单。只需调用
NumberFormatter.localizedString(from:1,number:.ordinal)//1st
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);