Skip to content

Instantly share code, notes, and snippets.

@jeffniblack
Created September 18, 2012 16:05
Show Gist options
  • Save jeffniblack/3743957 to your computer and use it in GitHub Desktop.
Save jeffniblack/3743957 to your computer and use it in GitHub Desktop.
NSDate+DateUtils
@interface NSDate (DateUtils)
- (NSDate *)toLocalTime;
- (NSString *)toLocalShortTime;
- (NSString *)toLocalShortDate;
- (NSString *)toLocalShortDate2;
- (NSString *)toLocalHeaderDate;
- (NSString *)toLocalArticleDetailDate;
@end
#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