从UIDatePicker提取日期 [英] Extract date from UIDatePicker
问题描述
您能告诉我如何从UIDatePicker中提取日期吗,我已经提取了月份,但是我无法提取日期,请帮帮我
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 date,Please help me out
谢谢
推荐答案
只需访问对象的date
属性即可提取NSDate
对象:
The NSDate
object can be extracted by just accessing the date
property of the object:
NSDate *dateFromPicker = [someDatePicker date];
如果要提取有关某人选择的月份,日期和年份的详细信息,则要稍微复杂一些(但仅需一点点).解决方案是使用NSCalendar参考-您可能想使用Gregorian.
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];
从此日期开始,您现在可以提取在unitFlags整数中提到的整数值:
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];
希望这会有所帮助. 有关更多信息,请参阅Apple参考: http://developer.apple.com/library/ios/#documentation/可可/Conceptual/DatesAndTimes/DatesAndTimes.html
Hope this helps. For more information, see Apple reference: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html
这篇关于从UIDatePicker提取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!