Created
September 18, 2012 16:05
-
-
Save jeffniblack/3743957 to your computer and use it in GitHub Desktop.
NSDate+DateUtils
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
@interface NSDate (DateUtils) | |
- (NSDate *)toLocalTime; | |
- (NSString *)toLocalShortTime; | |
- (NSString *)toLocalShortDate; | |
- (NSString *)toLocalShortDate2; | |
- (NSString *)toLocalHeaderDate; | |
- (NSString *)toLocalArticleDetailDate; | |
@end |
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 "NSDate+DateUtils.h" | |
@implementation NSDate (DateUtils) | |
- (NSDate *)toLocalTime { | |
NSTimeZone *zone = [NSTimeZone systemTimeZone]; | |
NSInteger seconds = [zone secondsFromGMTForDate:self]; | |
return [NSDate dateWithTimeInterval:seconds sinceDate:self]; | |
} | |
- (NSString *)toLocalShortTime { | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateStyle:NSDateFormatterNoStyle]; | |
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; | |
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
- (NSString *)toLocalShortDate { | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"yyyyMMdd"]; | |
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
- (NSString *)toLocalShortDate2 { | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"M/dd/yy"]; | |
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
- (NSString *)toLocalHeaderDate { | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"EEEE, MMM d"]; | |
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
- (NSString *)toLocalArticleDetailDate { | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"MMM d, yyyy"]; | |
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment