2015-10-16 66 views
0

我想在UIDatePicker中禁用24-时钟格式,我的要求是只显示AM/PM格式的日期选择器。如果用户在手机设置中启用24小时制时间。我的日期选取器显示24小时格式,我想迫使它显示12小时格式。请建议我。如何在UIDatePicker中禁用24小时格式

谢谢,

回答

5

日期格式取决于您的设备区域设置。

虽然它不是在大多数情况下,建议,您可以覆盖区域设置:

例如这给你24小时格式:

self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];

与PM这12小时格式/ AM

self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];

+0

@nikey您可以设置24小时制的[NSLocale localeWithLocaleIdentifier:@“en_GB”]或使用AM/PM选项设置12小时格式的[NSLocale localeWithLocaleIdentifier:@“en_US”]。 如果您可以使用定时器模式设置日期选择器,那么使用AM/PM的12小时格式是默认格式。 谢谢 –

1

你只是不能设置12/24格式。

这取决于用户在iOS设备上的设置。

+0

你的意思是,我们不能自定义日期选择器。 – nikey

+0

不,我们不能。 UIDatePicker与系统配置有关。苹果必须这样做才能确保用户的体验 – ronan

2

我们只能自定义当前的日期将会在24小时格式,如果用户设定意味着按照iOS设备设置..否则,我们可以这样设置

NSDate *currDate = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 

NSString *dateString = [dateFormatter stringFromDate:currDate]; 

_todayDateLbl.text=dateString; 
#pragma get current time 

dateFormatter.dateFormat = @"hh:mm a"; 
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
NSLog(@"The Current Time is %@",[dateFormatter stringFromDate:currDate]); 
1

取决于语言环境,但您可以构建自己的日期选择器。

1

24小时格式尝试...

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"]; 
[self.datePicker setLocale:locale]; 
+0

他想禁用它,不启用它。 – Jasper

相关问题