1
NSDate *date = [NSDate date];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = @"MMMM FF yyyy";

NSString *dateStr = [dateFormat stringFromDate:date];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
timeFormat.dateFormat = @"HH:mm aa";

NSString *timeStr = [[timeFormat stringFromDate:date] lowercaseString];

NSLog(@"%@ at %@", dateStr, timeStr);

Now I got my solution like this "February 15 2014 at 04:30 pm" but I need like this "February 15th 2014 at 04:30 pm". That is, I want it to say "15th" instead of just "15".

How do I do that?

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Vadivelan
  • 13
  • 3
  • Isn't that a mixture between two formatting styles. As far as I know it should ether be "January 12" or "12th of January". But I'm not a native speaker so I could very well be wrong. – David Rönnqvist Feb 15 '14 at 11:24
  • seed this question: http://stackoverflow.com/questions/1283045/ordinal-month-day-suffix-option-for-nsdateformatter-setdateformat – lancy Feb 15 '14 at 11:26
  • See also [(wiki) Date format by country](http://en.wikipedia.org/wiki/Date_format_by_country) and [(wiki) Expressing dates in spoken English](http://en.wikipedia.org/wiki/Calendar_date#Expressing_dates_in_spoken_English) – David Rönnqvist Feb 15 '14 at 11:29
  • lancy : thanks your link work for me thanks and lord – Vadivelan Feb 15 '14 at 11:43

5 Answers5

3

You'll need to modify the NSDateFormatter literal, and this will only work for english localizations. You can also do the complete date/time formatting in one pass:

untested

NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];   // or whatever
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:tz];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:tz];
[formatter setCalendar:calendar];

NSDate *date = [NSDate date];

NSDateComponents *comps = [calendar components:NSDayCalendarUnit
                                      fromDate:date];

// 1 = st
// 2 = nd
// 3 = rd
// 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 = th
// 21 = st
// 22 = nd
// 23 = rd
// 24,25,26,27,28,29,30 = th
// 31 = st

NSString *suffix;
if (comps.day == 11) {
    suffix = @"th";
} else {
    switch (comps.day % 10) {
    case 1: suffix = @"st"; break;
    case 2: suffix = @"nd"; break;
    case 3: suffix = @"rd"; break;
    default: suffix = @"th"; break;
    }
}

formatter.dateFormat = [NSString stringWithFormat:@"MMMM d'%@' yyyy 'at' hh:mm aa", suffix];

NSString *timeStr = [formatter stringFromDate:date];
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • @holex Indeed; fixed. `FF` -> `d`. – trojanfoe Feb 15 '14 at 15:14
  • @trojanfoe, and a _suffix_ calculation method is incorrect as well (e.g. _11st_...???), the hour formatter gives you wrong timestamp on afternoons (e.g. _17:05 PM_...???), the _am/pm_ suffix requested in lowercase letters. in nutshell it is poor answer, however at least it is readable. – holex Feb 16 '14 at 17:32
  • @holex Yeah the 11th was a special case that I'd forgotten about, which is now fixed. I did say the code was untested and I don't think these mistakes make it a "poor answer". Anyway who care anyway? The OP has chosen a different answer already. – trojanfoe Feb 16 '14 at 18:33
1

try this.

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: @"|"];

NSDateFormatter *monthDayFormatter = [[NSDateFormatter alloc] init];
[monthDayFormatter setDateFormat:@"d"];

NSDateFormatter *dateFormateHeader = [[NSDateFormatter alloc]init];

int date_day = [[monthDayFormatter stringFromDate:[NSDate date]] intValue];
NSString *suffix = [suffixes objectAtIndex:date_day];

[dateFormateHeader setDateFormat:[NSString stringWithFormat:@"MMMM d'%@' yyyy' at 'hh:mm a",suffix]];

NSString *prefixDateString = [dateFormateHeader stringFromDate:[NSDate date]];

NSLog(@"%@",prefixDateString);
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
  • 1
    that is a nice copy from here: http://stackoverflow.com/questions/1283045/ordinal-month-day-suffix-option-for-nsdateformatter-setdateformat – holex Feb 16 '14 at 17:38
0

USe following method

NSString *myDateString = [self getDateFroMyFormation:[NSDate date]];
NSLog(@"%@", myDateString);

And method code is

-(NSString *)getDateFroMyFormation:(NSDate *)todayDate
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit ;
    NSDateComponents *dateComponents = [gregorian components:units fromDate:todayDate];
    NSInteger hour = [dateComponents hour];
    NSInteger minute = [dateComponents minute];
    NSInteger day = [dateComponents day];
    NSInteger year = [dateComponents year];

    NSDateFormatter *calMonth = [[NSDateFormatter alloc] init];
    [calMonth setDateFormat:@"MMMM"];

    NSDateFormatter *hourTimer = [[NSDateFormatter alloc] init];
    [hourTimer setDateFormat:@"a"];

    NSString *strDay = @"";
    if (day>0)
    {
        if (day == 1 || day == 21 || day == 31)
            strDay = [NSString stringWithFormat:@"%dst",day];
        else if (day == 2 || day == 22)
            strDay = [NSString stringWithFormat:@"%dnd",day];
        else if (day == 3 || day == 23)
            strDay = [NSString stringWithFormat:@"%drd",day];
        else
        {
            strDay = [NSString stringWithFormat:@"%dth",day];
        }
    }

    //"February 15th 2014 at 04:30 pm"
    NSString *strDateFormation = [NSString stringWithFormat:@"%@ %@ %d  at %d:%d  %@",  [calMonth stringFromDate:todayDate], strDay, year, hour, minute, [hourTimer stringFromDate:todayDate]];
    return strDateFormation;
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

if you'd prefer compact solutions, that makes the proper job for you:

- (NSString *)formattedDateFrom:(NSDate *)date {
    NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
    [_dateFormatter setPMSymbol:@"pm"];
    [_dateFormatter setAMSymbol:@"am"];
    [_dateFormatter setDateFormat:@"MMMM dd'%@' YYYY 'at' hh:mm aa"];
    return [NSString stringWithFormat:[_dateFormatter stringFromDate:date], [[@"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" componentsSeparatedByString:@","] objectAtIndex:[[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:date].day]];
}
holex
  • 23,961
  • 7
  • 62
  • 76
  • @trojanfoe, that is very true. :) but at least it is working well, and that is point, not like the others. – holex Feb 16 '14 at 17:25
  • Why build the array from the string when you could initialise (a possibly static) array once? Doing that would make the code much quicker. – trojanfoe Feb 16 '14 at 18:36
-1

NSDate *dateTemp = [NSDate date]; NSLog(@"Current date%@",dateTemp);

NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateStyle:NSDateFormatterMediumStyle];

NSLog(@"NSDateFormatterMediumStyle %@",[dateFormat2 stringFromDate:dateTemp]);
NSArray *arr =[[dateFormat2 stringFromDate:dateTemp] componentsSeparatedByString:@","];

NSLog(@"%@th %@",[arr objectAtIndex:0],[arr objectAtIndex:1]);
Parthi
  • 99
  • 6