HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在iOS中添加有序缩写格式到日期,例如“th”,“st”[复制]大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Ordinal Month-day Suffix Option for NSDateFormatter setDateFormat                                    15个
我有以下代码将我的日期和时间格式化为字符串,如:

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之后?

解决方法

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);

大佬总结

以上是大佬教程为你收集整理的如何在iOS中添加有序缩写格式到日期,例如“th”,“st”[复制]全部内容,希望文章能够帮你解决如何在iOS中添加有序缩写格式到日期,例如“th”,“st”[复制]所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。