Last active
March 1, 2019 23:07
-
-
Save PMUNOZ08/6725246 to your computer and use it in GitHub Desktop.
NSDate category to make easy to work with date components
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
| // | |
| // NSDate+Utilities.h | |
| // | |
| // Created by PEDRO MUÑOZ CABRERA on 15/03/13. | |
| // Copyright (c) 2013. All rights reserved. | |
| // | |
| #import <Foundation/Foundation.h> | |
| @interface NSDate (Utilities) | |
| @property (nonatomic, readonly) NSInteger year; | |
| @property (nonatomic, readonly) NSInteger month; | |
| @property (nonatomic, readonly) NSInteger day; | |
| @property (nonatomic, readonly) NSInteger hour; | |
| @property (nonatomic, readonly) NSInteger minute; | |
| @property (nonatomic, readonly) NSInteger second; | |
| + (NSDate *)dateFromString:(NSString *)dateStr usingFormat:(NSString *)format; | |
| + (NSDate *)justTimeFromString:(NSString *)dateStr usingFormat:(NSString *)format; | |
| + (BOOL)date:(NSDate*)date isBetweenDate1:(NSDate *)date1 andDate2:(NSDate *)date2; | |
| + (NSString *)timeStrFromTimeInterval:(NSTimeInterval)timeInterval; | |
| + (NSString *)timeStrDiferenceFromTimeInterval:(NSTimeInterval)timeInterval1 andTimeInterval2:(NSTimeInterval)timeInterval2; | |
| - (NSInteger) week; | |
| - (NSInteger) weekday; | |
| - (NSString*) monthName; | |
| - (NSDate *)dateFromString:(NSString *)dateStr usingFormat:(NSString *)format; | |
| - (NSString *)toStringUsingFormat:(NSString *)format; | |
| - (NSDate *)dateByAddingDay:(NSInteger)day; | |
| - (NSDate *)justTime; | |
| - (NSDate *)justDate; | |
| // Retrieving intervals | |
| - (NSInteger) minutesAfterDate: (NSDate *) aDate; | |
| - (NSInteger) minutesBeforeDate: (NSDate *) aDate; | |
| - (NSInteger) hoursAfterDate: (NSDate *) aDate; | |
| - (NSInteger) hoursBeforeDate: (NSDate *) aDate; | |
| - (NSInteger) daysAfterDate: (NSDate *) aDate; | |
| - (NSInteger) daysBeforeDate: (NSDate *) aDate; | |
| - (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate; | |
| @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
| // | |
| // NSDate+Utilities.m | |
| // | |
| // Created by PEDRO MUÑOZ CABRERA on 15/03/13. | |
| // Copyright (c) 2013. All rights reserved. | |
| // | |
| #import "NSDate+Utilities.h" | |
| #define kDefFormat @"dd-MM-yyyyy hh:mm" | |
| #define D_MINUTE 60 | |
| #define D_HOUR 3600 | |
| #define D_DAY 86400 | |
| #define D_WEEK 604800 | |
| #define D_YEAR 31556926 | |
| @interface NSDate(private) | |
| - (NSDateComponents *)components:(NSUInteger)components; | |
| @end | |
| @implementation NSDate (Utilities) | |
| - (NSInteger)year { | |
| return [self components:NSYearCalendarUnit].year; | |
| } | |
| - (NSInteger)month { | |
| return [self components:NSMonthCalendarUnit].month; | |
| } | |
| - (NSInteger)day { | |
| return [self components:NSDayCalendarUnit].day; | |
| } | |
| - (NSInteger)hour { | |
| return [self components:NSHourCalendarUnit].hour; | |
| } | |
| - (NSInteger)minute { | |
| return [self components:NSMinuteCalendarUnit].minute; | |
| } | |
| - (NSInteger)second { | |
| return [self components:NSSecondCalendarUnit].second; | |
| } | |
| - (NSInteger) week | |
| { | |
| return [self components:NSWeekdayCalendarUnit].week; | |
| } | |
| - (NSInteger) weekday | |
| { | |
| return [self components:NSWeekdayOrdinalCalendarUnit].weekday; | |
| } | |
| - (NSString*) monthName { | |
| NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
| [formatter setDateFormat:@"MMMM"]; | |
| [formatter setLocale:[NSLocale currentLocale]]; | |
| NSString *stringFromDate = [formatter stringFromDate:self]; | |
| return stringFromDate; | |
| } | |
| - (NSDate *)dateFromString:(NSString *)dateStr usingFormat:(NSString *)format { | |
| NSDateFormatter *df = [[NSDateFormatter alloc] init]; | |
| NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
| [df setTimeZone:gmt]; | |
| [df setDateFormat:format]; | |
| return [df dateFromString:dateStr]; | |
| } | |
| + (NSDate *)dateFromString:(NSString *)dateStr usingFormat:(NSString *)format { | |
| NSDateFormatter *df = [[NSDateFormatter alloc] init]; | |
| NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
| [df setTimeZone:gmt]; | |
| [df setDateFormat:format]; | |
| return [df dateFromString:dateStr]; | |
| } | |
| - (NSString *)toStringUsingFormat:(NSString *)format { | |
| NSDateFormatter *df = [[NSDateFormatter alloc] init]; | |
| NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
| [df setTimeZone:gmt]; | |
| [df setDateFormat:format]; | |
| return [df stringFromDate:self]; | |
| } | |
| - (NSDate *)dateByAddingDay:(NSInteger)day { | |
| return [NSDate date:self byAddingDay:day]; | |
| } | |
| - (NSDate *)justTime{ | |
| // | |
| // NSDateComponents *dc = [[NSDateComponents alloc] init]; | |
| // [dc setYear:0]; | |
| // [dc setMonth:0]; | |
| // [dc setDay:0]; | |
| // NSDate *dateModified = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:self options:0]; | |
| // | |
| // return dateModified; | |
| unsigned int flags = NSHourCalendarUnit | NSMinuteCalendarUnit; | |
| NSCalendar* calendar = [NSCalendar currentCalendar]; | |
| NSDateComponents* components = [calendar components:flags fromDate:self]; | |
| return [calendar dateFromComponents:components]; | |
| } | |
| - (NSDate *)justDate{ | |
| NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
| NSDateComponents* comps = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self]; | |
| [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; | |
| return [cal dateFromComponents:comps]; | |
| } | |
| + (NSDate *)date:(NSDate *)date | |
| byAddingYear:(NSInteger)year | |
| month:(NSInteger)month | |
| day:(NSInteger)day | |
| hour:(NSInteger)hour | |
| minute:(NSInteger)minute | |
| second:(NSInteger)second { | |
| NSDateComponents *dc = [[NSDateComponents alloc] init]; | |
| dc.year = year; | |
| dc.month = month; | |
| dc.day = day; | |
| dc.hour = hour; | |
| dc.minute = minute; | |
| dc.second = second; | |
| NSCalendar *c = [NSCalendar currentCalendar]; | |
| return [c dateByAddingComponents:dc | |
| toDate:date | |
| options:0]; | |
| } | |
| + (NSDate *)date:(NSDate *)date byAddingDay:(NSInteger)day { | |
| return [NSDate date:date | |
| byAddingYear:0 | |
| month:0 | |
| day:day | |
| hour:0 | |
| minute:0 | |
| second:0]; | |
| } | |
| + (NSDate *)justTimeFromString:(NSString *)dateStr usingFormat:(NSString *)format{ | |
| NSDate *fecha = [NSDate dateFromString:dateStr usingFormat:format]; | |
| NSString *timeStr = [fecha toStringUsingFormat:@"HH:mm"]; | |
| return [NSDate dateFromString:timeStr usingFormat:@"HH:mm"]; | |
| } | |
| + (BOOL)date:(NSDate*)date isBetweenDate1:(NSDate *)date1 andDate2:(NSDate *)date2{ | |
| if ((([date compare: date1] == NSOrderedDescending) || ([date compare: date1] == NSOrderedSame)) | |
| && (([date compare: date2] == NSOrderedAscending) || ([date compare: date2] == NSOrderedSame))) { | |
| return YES; | |
| } | |
| return NO; | |
| } | |
| - (NSDateComponents *)allComponents { | |
| return [self components: | |
| NSYearCalendarUnit | | |
| NSMonthCalendarUnit | | |
| NSDayCalendarUnit | | |
| NSWeekCalendarUnit | | |
| NSHourCalendarUnit | | |
| NSMinuteCalendarUnit | | |
| NSSecondCalendarUnit | | |
| NSWeekdayCalendarUnit | | |
| NSWeekdayOrdinalCalendarUnit]; | |
| } | |
| + (NSString *)timeStrFromTimeInterval:(NSTimeInterval)timeInterval{ | |
| NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970: timeInterval]; | |
| NSString *timeStr = [date toStringUsingFormat:@"HH:mm"]; | |
| return timeStr; | |
| } | |
| + (NSString *)timeStrDiferenceFromTimeInterval:(NSTimeInterval)timeInterval1 andTimeInterval2:(NSTimeInterval)timeInterval2{ | |
| NSTimeInterval tsDiff = timeInterval1 - timeInterval2; | |
| // Divide the interval by 3600 and keep the quotient and remainder | |
| div_t h = div(tsDiff, 3600); | |
| int hours = h.quot; | |
| // Divide the remainder by 60; the quotient is minutes, the remainder | |
| // is seconds. | |
| div_t m = div(h.rem, 60); | |
| int minutes = m.quot; | |
| NSString *timeStr =[NSString stringWithFormat:@"%dh %dm", hours, minutes]; | |
| return timeStr; | |
| } | |
| #pragma mark Retrieving Intervals | |
| - (NSInteger) minutesAfterDate: (NSDate *) aDate | |
| { | |
| NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; | |
| return (NSInteger) (ti / D_MINUTE); | |
| } | |
| - (NSInteger) minutesBeforeDate: (NSDate *) aDate | |
| { | |
| NSTimeInterval ti = [aDate timeIntervalSinceDate:self]; | |
| return (NSInteger) (ti / D_MINUTE); | |
| } | |
| - (NSInteger) hoursAfterDate: (NSDate *) aDate | |
| { | |
| NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; | |
| return (NSInteger) (ti / D_HOUR); | |
| } | |
| - (NSInteger) hoursBeforeDate: (NSDate *) aDate | |
| { | |
| NSTimeInterval ti = [aDate timeIntervalSinceDate:self]; | |
| return (NSInteger) (ti / D_HOUR); | |
| } | |
| - (NSInteger) daysAfterDate: (NSDate *) aDate | |
| { | |
| NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; | |
| return (NSInteger) (ti / D_DAY); | |
| } | |
| - (NSInteger) daysBeforeDate: (NSDate *) aDate | |
| { | |
| NSTimeInterval ti = [aDate timeIntervalSinceDate:self]; | |
| return (NSInteger) (ti / D_DAY); | |
| } | |
| - (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate | |
| { | |
| NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
| NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:self toDate:anotherDate options:0]; | |
| return components.day; | |
| } | |
| #pragma mark - Private | |
| - (NSDateComponents *)components:(NSUInteger)components { | |
| return [[NSCalendar currentCalendar] components:components | |
| fromDate:self]; | |
| } | |
| @end |
Author
Add new methods to make easier to convert DateTime to Fuzzy Time
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pillado ;)
Gracias Pedro