Showing posts with label Date Components. Show all posts
Showing posts with label Date Components. Show all posts

Thursday, June 9, 2011

How to get the Date Components from NSDate in Objective-C ?



NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:date];
NSInteger _day=[dateComponents day];

dateComponents = [[NSCalendar currentCalendarcomponents:NSMonthCalendarUnit fromDate:date];
NSInteger _month=[dateComponents month];








Similarly, you can get the other components like year, hour, seconds etc , you will have to change components: to relevant CalendarUnit and you can access them with [dateComponents year], [dateComponents hour] etc.