How to change the format of date in date picker

折月煮酒 提交于 2019-11-26 10:01:56

问题


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

\"enter

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;
}


来源:https://stackoverflow.com/questions/13952063/how-to-change-the-format-of-date-in-date-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!