是否可以将日期名称添加到日期选择器? 例如拨号将显示:
(星期日)3 | 11 | 2012
有可能吗? 谢谢!
最佳答案
我认为你不能将 DayName(例如星期日)与 Year-Month-Day 一起使用,除非你完全自定义它,但你可以通过以下代码获取带日期的日期可能会对你有所帮助。
在Xib中绑定(bind)以下两个IBOutlet。
并将方法 -(IBAction)GetDateWithDay 与 DatePicker 和 ValueChange 属性绑定(bind)
在.h文件中
IBOutlet UIDatePicker* datePicker;
IBOutlet UILabel* lblDate;
在 .m 文件中
-(IBAction)GetDateWithDay
{
NSDate* dt = datePicker.date;
NSDateFormatter* df = [[[NSDateFormatter alloc]init]autorelease];
[df setDateFormat:@"yyyy-MM-dd"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:dt];
NSInteger year = [components year];
NSInteger day = [components day];
NSDateFormatter *weekDay = [[[NSDateFormatter alloc] init] autorelease];
[weekDay setDateFormat:@"EEEE"];
NSDateFormatter *calMonth = [[[NSDateFormatter alloc] init] autorelease];
[calMonth setDateFormat:@"MM"];
lblDate.text = [NSString stringWithFormat:@"%@, %i-%@-%i",[weekDay stringFromDate:dt], day, [calMonth stringFromDate:dt], year];
关于iphone - 日期选择器是否可以显示年/月/日 + 日名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10532070/