Last active
April 14, 2016 14:58
-
-
Save ylem/deeee95fa291c308ff358c0d55ce3827 to your computer and use it in GitHub Desktop.
show 1st, 2nd, 3rd, 4th... day format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface Utlity : NSObject | |
+ (NSString *)formatDaySuffixForDate:(NSDate *)date; | |
+ (NSString *)formatDayWithSuffixForDate:(NSDate *)date withWeekday:(BOOL)showWeekday; | |
@end | |
@implementation Utlity | |
+ (NSString *)formatDaySuffixForDate:(NSDate *)date { | |
NSCalendar *calendar = [NSCalendar currentCalendar]; | |
NSInteger dayOfMonth = [calendar component:NSCalendarUnitDay fromDate:date]; | |
NSString *result = @"th"; | |
switch (dayOfMonth % 10) { | |
case 1: | |
result = @"st"; | |
break; | |
case 2: | |
result = @"nd"; | |
break; | |
case 3: | |
result = @"rd"; | |
break; | |
default: | |
result = @"th"; | |
} | |
return [NSString stringWithFormat:@"%@%@", @(dayOfMonth), result]; | |
} | |
+ (NSString *)formatDayWithSuffixForDate:(NSDate *)date withWeekday:(BOOL)showWeekday { | |
NSString *day = [Utility formatDaySuffixForDate:date]; | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
formatter.dateFormat = @"MMM"; | |
NSString *month = [formatter stringFromDate:date]; | |
if (showWeekday) { | |
formatter.dateFormat = @"EEE"; | |
NSString *weekday = [formatter stringFromDate:date]; | |
// Mon 21st Jun | |
return [NSString stringWithFormat:@"%@ %@ %@", weekday, day, month]; | |
} | |
// 21st Jun | |
return [NSString stringWithFormat:@"%@ %@", day, month]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment