Skip to content

Commit

Permalink
I will concede this is kinda lazy.
Browse files Browse the repository at this point in the history
  • Loading branch information
billymeltdown committed Apr 21, 2011
1 parent 8e80c1a commit cd83dee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions NSDate+Helper.h
Expand Up @@ -44,6 +44,7 @@
+ (NSString *)stringFromDate:(NSDate *)date;
+ (NSString *)stringForDisplayFromDate:(NSDate *)date;
+ (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed;
+ (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed alwaysDisplayTime:(BOOL)displayTime;

- (NSString *)string;
- (NSString *)stringWithFormat:(NSString *)format;
Expand Down
25 changes: 20 additions & 5 deletions NSDate+Helper.m
Expand Up @@ -111,8 +111,9 @@ + (NSString *)stringFromDate:(NSDate *)date {
return [date string];
}

+ (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed {
/*
+ (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed alwaysDisplayTime:(BOOL)displayTime
{
/*
* if the date is in today, display 12-hour time with meridian,
* if it is within the last 7 days, display weekday name (Friday)
* if within the calendar year, display as Jan 23
Expand Down Expand Up @@ -141,7 +142,10 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed {
NSDate *lastweek = [calendar dateByAddingComponents:componentsToSubtract toDate:today options:0];
[componentsToSubtract release];
if ([date compare:lastweek] == NSOrderedDescending) {
[displayFormatter setDateFormat:@"EEEE"]; // Tuesday
if (displayTime)
[displayFormatter setDateFormat:@"EEEE h:mm a"]; // Tuesday
else
[displayFormatter setDateFormat:@"EEEE"]; // Tuesday
} else {
// check if same calendar year
NSInteger thisYear = [offsetComponents year];
Expand All @@ -150,9 +154,15 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed {
fromDate:date];
NSInteger thatYear = [dateComponents year];
if (thatYear >= thisYear) {
[displayFormatter setDateFormat:@"MMM d"];
if (displayTime)
[displayFormatter setDateFormat:@"MMM d h:mm a"];
else
[displayFormatter setDateFormat:@"MMM d"];
} else {
[displayFormatter setDateFormat:@"MMM d, yyyy"];
if (displayTime)
[displayFormatter setDateFormat:@"MMM d, yyyy h:mm a"];
else
[displayFormatter setDateFormat:@"MMM d, yyyy"];
}
}
if (prefixed) {
Expand All @@ -167,6 +177,11 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed {
return displayString;
}

+ (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed {
// preserve prior behavior
return [self stringForDisplayFromDate:date prefixed:prefixed alwaysDisplayTime:NO];
}

+ (NSString *)stringForDisplayFromDate:(NSDate *)date {
return [self stringForDisplayFromDate:date prefixed:NO];
}
Expand Down

0 comments on commit cd83dee

Please sign in to comment.