Calendrical Calculations
suggest change- components:fromDate: returns an NSDateComponents object containing a given date decomposed into specified components.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"%@",[calendar components:NSCalendarUnitDay fromDate:[NSDate date]]);
NSLog(@"%@",[calendar components:NSCalendarUnitYear fromDate:[NSDate date]]);
NSLog(@"%@",[calendar components:NSCalendarUnitMonth fromDate:[NSDate date]]);
- components:fromDate:toDate:options: returns, as an NSDateComponents object using the specified components, the difference between two dates.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"%@",[calendar components:NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSince1970:0] toDate:[NSDate dateWithTimeIntervalSinceNow:18000] options:NSCalendarWrapComponents]);
- dateByAddingComponents:toDate:options: returns a new NSDate representing the absolute time calculated by adding the given components to a given date.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComponent = [[NSDateComponents alloc] init];
[dateComponent setYear:10];
NSLog(@"%@",[calendar dateByAddingComponents:dateComponent toDate:[NSDate dateWithTimeIntervalSinceNow:0] options:NSCalendarWrapComponents]);
- dateFromComponents: returns a new NSDate representing the absolute time calculated from the given components.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComponent = [[NSDateComponents alloc] init];
[dateComponent setYear:2020];
NSLog(@"%@",[calendar dateFromComponents:dateComponent]);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents