Created
July 18, 2009 17:52
-
-
Save shillcock/149633 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 NSDate(Misc) | |
| + (NSDate *)dateWithoutTime; | |
| - (NSDate *)dateByAddingDays:(NSInteger)numDays; | |
| - (NSDate *)dateAsDateWithoutTime; | |
| - (int)differenceInDaysTo:(NSDate *)toDate; | |
| - (NSString *)formattedDateString; | |
| - (NSString *)formattedStringUsingFormat:(NSString *)dateFormat; | |
| @end |
This file contains hidden or 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-Misc.h" | |
| @implementation NSDate(Misc) | |
| + (NSDate *)dateWithoutTime | |
| { | |
| return [[NSDate date] dateAsDateWithoutTime]; | |
| } | |
| -(NSDate *)dateByAddingDays:(NSInteger)numDays | |
| { | |
| NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
| NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
| [comps setDay:numDays]; | |
| NSDate *date = [gregorian dateByAddingComponents:comps toDate:self options:0]; | |
| [comps release]; | |
| [gregorian release]; | |
| return date; | |
| } | |
| - (NSDate *)dateAsDateWithoutTime | |
| { | |
| NSString *formattedString = [self formattedDateString]; | |
| NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
| [formatter setDateFormat:@"MMM dd, yyyy"]; | |
| NSDate *ret = [formatter dateFromString:formattedString]; | |
| [formatter release]; | |
| return ret; | |
| } | |
| - (int)differenceInDaysTo:(NSDate *)toDate | |
| { | |
| NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
| NSDateComponents *components = [gregorian components:NSDayCalendarUnit | |
| fromDate:self | |
| toDate:toDate | |
| options:0]; | |
| NSInteger days = [components day]; | |
| [gregorian release]; | |
| return days; | |
| } | |
| - (NSString *)formattedDateString | |
| { | |
| return [self formattedStringUsingFormat:@"MMM dd, yyyy"]; | |
| } | |
| - (NSString *)formattedStringUsingFormat:(NSString *)dateFormat | |
| { | |
| NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
| [formatter setDateFormat:dateFormat]; | |
| NSString *ret = [formatter stringFromDate:self]; | |
| [formatter release]; | |
| return ret; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment