这是 12 月 18 日星期五的 NSDateFormatter 样式 [英] Which is this NSDateFormatter style Friday, December 18th

查看:52
本文介绍了这是 12 月 18 日星期五的 NSDateFormatter 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是哪种日期格式 12 月 18 日星期五 也是这种标准日期格式,或者我必须努力工作才能得到它.

I want to know which is this date format Friday, December 18th and also is this standard date format or i have to some hard work to get this.

谢谢.

推荐答案

我认为 th 不可能通过格式化程序实现.虽然你可以这样做:

I don't think the th is possible to achieve via formatters. Although you can do it like this:

NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EEEE, MMMM d"];
    NSString *dateString = [dateFormat stringFromDate:today];
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *dtComp = [cal components:unitFlags fromDate:today];
    switch ([dtComp day]) {
        case 1:
        case 31:
        case 21:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ist",[dtComp day]]];
            break;
        case 2:
        case 22:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ind",[dtComp day]]];
                break;
        case 3:
        case 23:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ird",[dtComp day]]];
            break;
        default:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ith",[dtComp day]]];
            break;
    }

    [dateFormat release];

这篇关于这是 12 月 18 日星期五的 NSDateFormatter 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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