Can u please tell me how to extract a date from UIDatePicker, i have extracted the month but i am not able to extract the da开发者_JAVA技巧te,Please help me out
Thank You
The NSDate
object can be extracted by just accessing the date
property of the object:
NSDate *dateFromPicker = [someDatePicker date];
If you want to extract detailed information about what month, day and year a person has chosen, this is a little bit more complex (but just a little bit). The solution is to use an NSCalendar reference - it is likely that you'd want to use Gregorian.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
Now you should use an unsigned integer unit flags to specify which units you want to extract from a date (i.e. day, day-of-the-month, year, era etc.). Use a bitwise | to select multiple unit flags:
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit;
Now create a date components object:
NSDateComponents *components = [calendar components: unitFlags fromDate: dateFromPicker];
From this components date, you can now extract integer values of what you have mentioned in the unitFlags integer:
int year = [components year];
int month = [components month];
All other properties (i.e. day, day-of-the-month, era etc.) are undefined in such a components object - remember, you can only extract what you've explicitly said you want to extract.
Finally, free the calendar object to avoid memory leaks:
[calendar release];
Hope this helps. For more information, see Apple reference: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html
You can access the date
property of the UIDatePicker
class to get the date!
精彩评论