NSDateComponents encapsulates the components of a date in an extendable, object-oriented manner. This is available in Apple's Foundation Framework .It is Available in iOS 2.0 and later and for OSX v10.4 and later. The issues related with date components in apple's Cocoa and cocoa touch can be tagged with NSDateComponent tag
NSDateComponents
serves an important role in Foundation’s date and time APIs. By itself, it’s nothing impressive—just a container for information about a date (its month, year, day of month, week of year, or whether that month is a leap month).
NSDateComponents
can be initialized and manipulated manually, but most often, they’re extracted from a specified date, using NSCalendar -components:fromDate::
An NSDateComponents object is not required to define all the component fields. When a new instance of NSDateComponents is created the date components are set to NSUndefinedDateComponent
.
Source:
Sample :
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekOfYear:1];
[components setHour:12];
NSLog(@"1 week and twelve hours from now: %@", [calendar dateByAddingComponents:components toDate:date options:0]);