用于在2011年9月14日转换日期为2011年9月14日的日期格式程序 [英] Date formatter for converting 14-sept-2011 in 14th Sept

查看:72
本文介绍了用于在2011年9月14日转换日期为2011年9月14日的日期格式程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串2011年9月14日需要转换为9月14日.此日期可以是任何日期字符串.是否有任何日期格式化程序可以让我以自己的格式转换日期.好像日期是2011年9月1日,那么我需要9月1日,2011年9月2日应该说9月2日.

I have a string 14-Sep-2011 In need to convert this 14th Sept. This date may be any date string. Is there any date formatter which allows me to convert date in my format. As if date is 1-Sept-2011 then I need 1st Sept, 2-Sept-2011 should say 2nd Sept.

任何人都可以提出解决方案的建议.

Can anyone please suggest the solution.

谢谢

推荐答案

- (NSString *)ordinalSuffixFromInt:(int)number {
    NSArray *cSfx = [NSArray arrayWithObjects:@"th", @"st", @"nd", @"rd", @"th", @"th", @"th", @"th", @"th", @"th", nil];
    NSString *suffix = @"th";

    number = abs(number % 100);
    if ((number < 10) || (number > 19)) {
        suffix = [cSfx objectAtIndex:number % 10];
    }
    return suffix;
}

测试:

- (void)test {
    for (int day=1; day<=31; day++) {
        NSLog(@"ordinal: %d%@", day, [self ordinalSuffixFromInt:day]);
    }
}

这篇关于用于在2011年9月14日转换日期为2011年9月14日的日期格式程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆