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.

Thanks

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
  • 2
    Similar question http://stackoverflow.com/questions/1283045/ordinal-month-day-suffix-option-for-nsdateformatter-setdateformat – Joe Sep 14 '11 at 13:17
  • Where is this date coming from? `my be any date string` is a lot of possible strings to try to parse! – deanWombourne Sep 14 '11 at 13:45
  • If it's the suffix (st, nd etc) you're after, then the question Joe linked to has the answer. – jrturton Sep 14 '11 at 13:57
  • Any date mean any calender date.. Format will be same dd-mmm-yyyy.. I don't want to parse this. I want to know whether there is any format with `NSDateFormatter` by which I can get dd(st/nd/rd/th) mmmm format string or I have to write manual logic thats it. – Kapil Choubisa Sep 14 '11 at 13:57

4 Answers4

2
- (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;
}

Test:

- (void)test {
    for (int day=1; day<=31; day++) {
        NSLog(@"ordinal: %d%@", day, [self ordinalSuffixFromInt:day]);
    }
}
zaph
  • 111,848
  • 21
  • 189
  • 228
0

This will work for any number :

 -(NSString *) ordinalSuffix: (NSInteger) day {
    NSString *ordinalSuffix;

    if(day%10 == 1 && day%100 != 11) ordinalSuffix = @"st";
    else if(day%10 == 2 && day%100 != 12) ordinalSuffix = @"nd";
    else if(day%10 == 3 && day%100 != 13) ordinalSuffix = @"rd";
    else ordinalSuffix = @"th";

    return ordinalSuffix;
 }

Test :

 -(void) test {
     for(NSInteger i = 1; i < 200; i++)
         NSLog(@"%d%@", i, [self ordinalSuffix:i]);
 }
Stephan
  • 41,764
  • 65
  • 238
  • 329
Ali Yousuf
  • 692
  • 8
  • 23
  • Date 11 has 11th not 11st.. same for 12,13 etc. You can see @Zaph I had marked already. and a month may have maximum date is 31st. Your solution won't work for that. Thanks anyway for replying :) – Kapil Choubisa Aug 08 '12 at 05:07
0

You try the following code because I run successfully.

    NSString *dateStr = @"14-Sep-2011";
    NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
    [dtF setDateFormat:@"dd-MMM-yyyy"];
    NSDate *d = [dtF dateFromString:dateStr];
    NSDateFormatter *monthDayFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [monthDayFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
    [monthDayFormatter setDateFormat:@"d MMM"];         
    int date_day = [[monthDayFormatter stringFromDate:d] 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];   
    NSString *format = [NSString stringWithFormat:@"%d",date_day];
    NSString *dateStrfff = [format stringByAppendingString:suffix];
    NSLog(@"%@", dateStrfff);
    [monthDayFormatter setDateFormat:@"MMM"];
    NSString *ss = [monthDayFormatter stringFromDate:d];
    NSLog(@"%@",ss);

    NSString *final = [dateStrfff stringByAppendingString:ss];
    NSLog(@"final string:---> %@",final);

This is a perfect solution that you want.

Nikunj Jadav
  • 3,417
  • 7
  • 38
  • 54
  • 1
    Glad you can follow the link to the duplicate question / answer, even if the submitter can't ;-) – jrturton Sep 14 '11 at 14:16
  • If I look at the amount of allocations just for this issue, this doesn't look like a perfect solution to me – Jasper Mar 30 '16 at 09:00
-1

use it

     NSString *string =@"14-Sep-2011";
    NSArray *arr = [string componentsSeparatedByString:@"-"];
    NSString *str1=[arr objectAtIndex:0];
    str1=[str1 stringByAppendingString:@"th"];
    NSString *final=[str1 stringByAppendingFormat:@" %@",[arr objectAtIndex:1]];
Deepesh
  • 633
  • 4
  • 11
  • This is not perfect solution.. because every date don't accept `th` some require `st`, `nd`, `rd`. I can do this by checking conditions as you told but I want to know if there is any `NSDateFormatter` format to set.. – Kapil Choubisa Sep 14 '11 at 13:38