Skip to content

Instantly share code, notes, and snippets.

@JigsChanchiya
Last active January 28, 2016 02:52
Show Gist options
  • Save JigsChanchiya/6044680 to your computer and use it in GitHub Desktop.
Save JigsChanchiya/6044680 to your computer and use it in GitHub Desktop.
date formatting with suffix
-(NSString *)StringFromDate:(NSDate *)DateLocal{
NSDateFormatter *prefixDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[prefixDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[prefixDateFormatter setDateFormat:@"MMMM d., yyyy"];//June 13th, 2013
NSString * prefixDateString = [prefixDateFormatter stringFromDate:DateLocal];
NSDateFormatter *monthDayFormatter = [[[NSDateFormatter alloc] init] autorelease];
[monthDayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[monthDayFormatter setDateFormat:@"d"];
int date_day = [[monthDayFormatter stringFromDate:DateLocal] 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];
prefixDateString = [prefixDateString stringByReplacingOccurrencesOfString:@"." withString:suffix];
NSString *dateString =prefixDateString;
// NSLog(@"%@", dateString);
return dateString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment