-1

I have some code like this:

NSString *dateStr = @"23 03 2013";
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"d MM yyyy"];
NSDate *date1 = [df1 dateFromString:dateStr];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:@"MMMM d YYYY"];
NSString *s = [df2 stringFromDate:date1];
NSLog(@"my new Date is %@",s);

//Output : March 23 2013

But I want like this: March 23rd 2013, How to solve it?

Sovannarith
  • 614
  • 1
  • 10
  • 20
  • If possible, don't format a date like this. First, it will break when a non-English locale is selected. Second, English doesn't write the ordinals explicitly to the date. In most situations setting format using `setDateStyle:` is more suitable and portable than using `setDateFormat:`. `setDateFormat:` is best used with parsing, not when displaying dates to the user. – Sulthan Mar 25 '13 at 09:55

2 Answers2

3

Use:

[df2 setDateFormat:@"MMMM d'rd' YYYY"];

*Note, for 21st, 22nd, rd, th, etc you need to check using condition and then change the formatter

NSString *dateStr = @"23 03 2013";
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"d MM yyyy"];
NSDate *date1 = [df1 dateFromString:dateStr];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];

NSDateFormatter *dfDate=[NSDateFormatter new];
[dfDate setDateFormat:@"dd"];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date1];


NSInteger dayValue=[components day];
/*
switch (dayValue) {
    case 1:
    case 21:
    case 31:
        [df2 setDateFormat:@"MMMM d'st' YYYY"];
        break;
    case 2:
    case 22:
        [df2 setDateFormat:@"MMMM d'nd' YYYY"];
        break;
    case 3:
    case 23:
        [df2 setDateFormat:@"MMMM d'rd' YYYY"];
        break;
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 24:
    case 25:
    case 26:
    case 27:
    case 28:
    case 29:
    case 30:
        [df2 setDateFormat:@"MMMM d'th' YYYY"];
        break;

    default:
        break;
}
*/

NSArray *suffix=@[@"st",@"nd",@"rd",
              @"th",@"th",@"th",@"th",@"th",@"th",@"th",
              @"th",@"th",@"th",@"th",@"th",@"th",@"th",@"th",@"th",
              @"th",
              @"st",@"nd",@"rd",@"th",@"th",@"th",@"th",@"th",@"th",@"th",@"st"];
NSLog(@"%d",[suffix count]);

[df2 setDateFormat:[NSString stringWithFormat:@"MMMM d'%@' YYYY",suffix[dayValue - 1]]];

NSString *s = [df2 stringFromDate:date1];
NSLog(@"my new Date is %@",s);

NOTE: I used switch, you can refactor it using arrays.

Now you can

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
1

directly its not available in IOS, but i achieved it using following code

NSString *dateStr = @"23 03 2013";
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"d MM yyyy"];
NSDate *date1 = [df1 dateFromString:dateStr];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:@"MMMM d YYYY"];
NSString *s = [df2 stringFromDate:date1];

NSDateFormatter *monthDayFormatter = [[NSDateFormatter alloc] init];
[monthDayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[monthDayFormatter setDateFormat:@"d"];
int date_day = [[monthDayFormatter stringFromDate:date1] intValue];
NSString *suffix_string = @"|st|nd|rd|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|st|nd|rd|th|th|th|th|th|th|th|st";
NSArray *suffixes = [suffix_string componentsSeparatedByString: @"|"];
NSString *suffix = [suffixes objectAtIndex:date_day];

NSLog(@"Suffix %@",suffix);
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57