I need to have the following date formatted while using the application with an US locale: January 1st, July 2nd, November 28th
How should I do that?
Searched with NSDateFormatter
, but I'm not able to set an appropriate format and default formats are too long.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMMM dd'st'"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(@"%@",dateString);
this will give you the required format but you need to add some condition to add proper 'st','nd','rd' etc.
like this
might this be helpful to you
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMMM dd"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:[now dateByAddingTimeInterval:(-60*60*24*10)]];
NSMutableString *tempDate = [[NSMutableString alloc]initWithString:dateString];
int day = [[tempDate substringFromIndex:[tempDate length]-2] intValue];
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);