Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is this NSDateFormatter style Friday, December 18th

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.

Thanks.

like image 308
Robin Avatar asked Dec 09 '22 10:12

Robin


2 Answers

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];
like image 146
Madhup Singh Yadav Avatar answered Dec 29 '22 00:12

Madhup Singh Yadav


@Robin Help with NSDateFormatter is something which will help u a lot in this case of NSDateFormatter, you really have to work hard to get this format,Just see the link i had shared with you.

Good Luck!

like image 32
Sudhanshu Avatar answered Dec 29 '22 02:12

Sudhanshu