CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✗ |
| LangLanguage | Obj-CObjective C |
| License | MIT |
| ReleasedLast Release | Dec 2014 |
Maintained by Rich Hodgkins.
NSDateComponents.NSDateComponents converted to a NSDictionary representation for quick use.If you want to use a different calendar, other than [NSCalendar currentCalendar], the methods all have a similar method where a NSCalendar can be passed in.
Docset for Dash
Instead of this:
NSDate *now = [NSDate date];
// Create components to shift date by 5 years
NSDateComponents *comps = [NSDateComponents new];
[comps setYear:5];
NSDate *fiveYearsFromNow = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:now options:0];Can be replaced by:
NSDate *now = [NSDate date];
NSDate *fiveYearsFromNow = [now dateByUsingCurrentCalendarToAdjustCalendarUnit:NSYearCalendarUnit withValue:5];This also demos the use of the dictionary represenation for quickly creating date components. Instead of this:
NSDate *now = [NSDate date];
// Create components to shift date by 1 year, 7 months, 15 hours, 36 seconds
NSDateComponents *comps = [NSDateComponents new];
[comps setYear:1];
[comps setMonth:7];
[comps setHour:15];
[comps setSecond:36];
NSDate *adjustedDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:now options:0];Can be replaced by:
NSDate *now = [NSDate date];
// Create components to shift date by 1 year, 7 months, 15 hours, 36 seconds
NSDictionary *compsDict = @{@(NSYearCalendarUnit) : 1,
@(NSMonthCalendarUnit) : 7,
@(NSHourCalendarUnit) : 15,
@(NSSecondCalendarUnit) : 36};
NSDate *adjustedDate = [now dateByUsingCurrentCalendarToAddDateComponentsDictionary:compsDict];Also creating components can be simplified:
// To get a NSDateComonents object
NSDateComponents *comps = [NSDateComponents dateWithCurrentCalendarFromDateComponentsDictionary:@{@(NSYearCalendarUnit) : 1,
@(NSMonthCalendarUnit) : 7,
@(NSHourCalendarUnit) : 15,
@(NSSecondCalendarUnit) : 36}];
// To go from NSDateComponents
NSDictionary *compsDict = [comps dictionaryRepresentation];Instead of this:
// Components for 2001-01-01, UTC+2
NSDateComponents *comps = [NSDateComponents new];
[comps setYear:2001];
[comps setMonth:1];
[comps setDay:1];
[comps setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:2 * 3600]];
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];Can be replaced by:
NSDate *date = [NSDate dateWithCurrentCalendarFromDateComponentsDictionary:@{
@(NSYearCalendarUnit) : @2001,
@(NSMonthCalendarUnit) : 1,
@(NSDayCalendarUnit) : 1,
@(NSTimeZoneUnit) : [NSTimeZone timeZoneForSecondsFromGMT:2 * 3600]}];Instead of this:
NSCalendar *cal = [NSCalendar currentCalendar];
// Remove time component
NSDateComponents *comps = [cal componentsFromDate:[NSDate date]];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSDate *date = [cal dateFromComponents:comps];Can be replaced by:
NSDate *date = [[NSDate date] dateOfStartOfDayUsingCurrentCalendar];