简体   繁体   English

更改区域中的UIDate Picker formate问题

[英]UIDate Picker formate issue in changing region

i am creating date picker like this: 我正在创建这样的日期选择器:

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;

my getCurrentLocal: 我的getCurrentLocal:

-(NSLocale *)getCurrentLocale
{
NSLocale *enUSPOSIXLocale;
enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
return enUSPOSIXLocale;
}

i am expecting UIDatePicker to always show daye in MM-dd-yyyy. 我希望UIDatePicker始终以MM-dd-yyyy显示daye。

Interesting: when i go to iPad setting > general > and change region to United States, my code works perfectly that is always UIDatePicker shows in MM-dd-yyyy.If i change region to any thing else (India), UIdate Picker shows date in dd-MM-yyyy why? 有趣的是:当我进入iPad设置>常规>并将区域更改为美国时,我的代码可以正常工作,并且始终以MM-dd-yyyy的UIDatePicker显示。如果我将区域更改为其他任何内容(印度),则UIdate Picker会显示日期在dd-MM-yyyy中为什么?

i always want to show in MM-dd-yyyy. 我一直想以MM-dd-yyyy显示。

Thanks 谢谢

Try This 尝试这个

 NSLocale *indain = [[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"];
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setLocale:indain];
    [datePicker setCalendar:cal];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.date = [NSDate date];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM