1

In my app I am using the date format like 'Thurs 10th April'. But I am getting the date format like 'Thurs 10 April'.I don't know how to change the format for this.

For First of January- It should come like 'Jan 1st'. For Second of August -It should come like 'Aug 2nd'. For Third of April -It should come like 'April 3rd'.

I want the date in this format. I am new to Xcode. Please can anyone help to solve this.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
iworld
  • 345
  • 1
  • 4
  • 17
  • 2
    Please refer below link for your solution.. http://stackoverflow.com/questions/10103654/ios-date-formating – Shah Nilay Dec 12 '14 at 06:22
  • you are new to xcode or objective-c ?? – Code cracker Dec 12 '14 at 06:30
  • @iworld Do you want date of the format _Thurs 10th April_ and likewise OR _April 10th_ and likewise? – Rumin Dec 12 '14 at 07:13
  • Whatever with date should be like 10th. – iworld Dec 12 '14 at 07:23
  • @iworld I have posted the answer for date of the format _Thurs 10th April_ . – Rumin Dec 12 '14 at 07:28
  • Why do you need this specific date string? Keep in mind that there are many countries in the world that don't use that strange date format. [You should not hard code that formats like this, there are ways to make the date format native on all locales](http://stackoverflow.com/a/5132177/457406). I don't want an app that tells me today is `Dezember 12th`. This just doesn't make any sense, because today is `12. Dezember`. There are billions of people that don't use the "Jan 1st" format. So please reconsider if you really _need_ this format. – Matthias Bauch Dec 12 '14 at 07:46
  • Yes ofcourse but it is client requirement. – iworld Dec 12 '14 at 08:33
  • Where did you get stuck? I thought you might have got it done. – Anon Dec 12 '14 at 11:10
  • @Nilay Shah 'th' format is coming but 'st' and 'rd' and 'nd' formants are not coming – iworld Dec 13 '14 at 06:33

3 Answers3

2

Have a look at the lovely, if complicated, NSDateFormatter class. While you are there, meet her friends, NSLocale, NSTimeZone, NSCalendar, NSDate, and NSDateComponents. Also be sure to read the Date & Time Programming guide from Apple (and read it again.)

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
2

I am assuming that you have your date ready to be used in mutable string. If So, you can use the following to get the output you wanted.

NSMutableString *mutableStrDate = // Your date in Mutable String Format.
int day = [[mutableStrDate substringFromIndex:[mutableStrDate length] - 2] intValue];

switch (day) 
{
    case 1:
    case 21:
    case 31:
        [mutableStrDate appendString:@"st"];
        break;
    case 2:
    case 22:
        [mutableStrDate appendString:@"nd"];
        break;
    case 3:
    case 23:
        [mutableStrDate appendString:@"rd"];
        break;
    default:
        [mutableStrDate appendString:@"th"];
        break;
}
NSLog(@"Formatted Date with %@",mutableStrDate);

Hope this helps.

Anon
  • 623
  • 3
  • 10
1

Here is what you can do:

  1. Divide the string into components and store them in array.

  2. Fetch the day part from that array and do some checking on it.

  3. Append appropriate day suffix to that day.

  4. Finally append entire string.

Below is the code:

NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"EEE dd MMMM"];

    NSDate *now = [NSDate date];

    NSString *dateString = [format stringFromDate:now];
    NSArray*tempString1 = [dateString componentsSeparatedByString:@" "];

    NSMutableString *tempDate = [[NSMutableString alloc]initWithString:[tempString1 objectAtIndex:1]];
    int day = [[tempString1 objectAtIndex:1] intValue];
    NSLog(@"%d",day);

    switch (day) {
        case 1:
            case 21:
        case 31:
            [tempDate appendString:@"st"];
            break;
        case 2:
            case 22:
            [tempDate appendString:@"nd"];
            break;
        case 3:
            case 23:
            [tempDate appendString:@"rd"];
            break;
        default:
            [tempDate appendString:@"th"];
            break;
    }
    NSLog(@"%@",tempDate);

    NSString *str1 = [[tempString1 objectAtIndex:0] stringByAppendingString:@" "];

    NSString *str2 = [str1 stringByAppendingString:tempDate];
    NSString *str3 = [str2 stringByAppendingString:@" "];

    NSString *finalDate = [str3 stringByAppendingString:[tempString1 objectAtIndex:2]];
    NSLog(@"%@",finalDate);
Rumin
  • 3,787
  • 3
  • 27
  • 30