How to change the format of date in date picker

2019-01-07 00:48发布

问题:

In my iPhone app, I need to display date in yyyy/MM/dd format as show in beloow image

but in iPhone Date picker it is in the format of MM/dd/yyyy which is not in uniform for my application

I tried with changing the local values (with different country regions) of the Date picker but no use it still in the default format.

How can I Change the date format on Date picker as per my requirement?

Any help is appreciated.

回答1:

In Apple's documentation they specified that in UIDatePickerModeDate. The DatePicker displays months, days of the month, and years. The exact order of these items depends on the locale setting. An example of this mode is [ November | 15 | 2007 ]. But In device it display as DD/MM/YYYY. See UIDatePicker for more info.

So if you want to do it yourself you could create your own UIPickerView with the values you want.



回答2:

The UIDatePicker is made to adapt to the user's settings. Changing its format using the locale: method was possible but is now deprecated in iOS 5.0.

You can view your format in the settings of your iPhone :

Settings > General > International > Region Format >

If you want a specific format, consider making your own picker but it is very discouraged for a date picker.

And also refer this Link



回答3:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:@"day, MMM HH:mm:ss Z"];
NSDate *dateT = [dateFormatter dateFromString:str1];
NSString *strDateTaken=[appDelegate convertDate:dateT withFormat:@"EEEE, MMMM dd,yyyy hh:mm a"];
[dateFormatter release];
- (NSString *)convertDate:(NSDate *)date withFormat:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString *dt = [formatter stringFromDate:date];
    [formatter release];
    return dt;
}