我正在创建这样的日期选择器:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker setFrame:self.txtFieldDatePicker.frame];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[self getCurrentLocale]];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSDate *dateForPicker = [dateFormatter dateFromString:self.txtFieldDatePicker.text];
if (dateForPicker)
{
[datePicker setDate:dateForPicker];
}
datePicker.datePickerMode = UIDatePickerModeDate;
objc_setAssociatedObject(datePicker,
(void*)MyConstKeyForDatePicker,
self.txtFieldDatePicker,
OBJC_ASSOCIATION_RETAIN);
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
// datePicker.tag = txtField.tag+1;
self.txtFieldDatePicker.inputAccessoryView = toolbar;
self.txtFieldDatePicker.inputView = datePicker;
我的getCurrentLocal:
-(NSLocale *)getCurrentLocale
{
NSLocale *enUSPOSIXLocale;
enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
return enUSPOSIXLocale;
}
我期待UIDatePicker始终在MM-dd-yyyy中显示日期。
有趣: 当我进入iPad设置>一般>并将区域更改为美国,我的代码完美无缺,UIDatePicker始终在MM-dd-yyyy中显示。如果我将区域更改为任何其他内容(印度),UIdate Picker会以dd-MM-yyyy显示日期为何?
我总是希望在MM-dd-yyyy中展示。
由于
答案 0 :(得分:0)
试试这个
NSLocale *indain = [[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"];
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setLocale:indain];
[datePicker setCalendar:cal];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.date = [NSDate date];