获取用户的日期格式? (DMY,MDY,YMD) [英] Get the user's date format? (DMY, MDY, YMD)

查看:851
本文介绍了获取用户的日期格式? (DMY,MDY,YMD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个表单,用户可以在其中输入带有3个UITextField的生日:1表示月份,1表示日期,1表示年份.我想根据用户的日期格式进行排列.
即:美国用户为[MONTH] [DAY] [YEAR],欧洲用户为[DAY] [MONTH] [YEAR],等等.
日期格式的国家/地区地图: http://en.wikipedia.org/wiki/Date_format_by_country#Map

I'm creating a form where the user can enter their birthday with 3 UITextFields: 1 for month, 1 for day and 1 for year. I'd like to arrange them based on the user's date format.
i.e.: [ MONTH ] [ DAY ] [ YEAR ] for American users, [ DAY ] [ MONTH ] [ YEAR ] for European users, etc.
Map of the date format by country: http://en.wikipedia.org/wiki/Date_format_by_country#Map

获取该格式最简单的方法是什么?

What's the easiest way to get that format?

现在,我将日期设置为2000年10月20日,并解析字符串.

Right now I'm setting the date October, 20 2000 and parsing the string.

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:20];
[components setMonth:10];
[components setYear:2000];
NSDate *date = [calendar dateFromComponents:components];

NSLog(@"%@", [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]);

但是我不认为这是正确的选择,因为我已经有太多不同的格式:

But I don't think it's the right thing to do since I got already too many different formats:

美国:10/20/00
FR:20/10/00
日本:2000/10/20
瑞典:2000-10-20

US: 10/20/00
FR: 20/10/00
Japan: 2000/10/20
Sweden: 2000-10-20

推荐答案

您可以使用NSDateFormatter dateFormatFromTemplate:options:locale:.

NSString *base = @"MM/dd/yyyy";
NSLocale *locale = [NSLocale currentLocale];
NSString *format = [NSDateFormatter dateFormatFromTemplate:base options:0 locale:locale];

format的值将根据区域设置以正确的格式进行调整.现在的诀窍是扫描format以查看找到Mdy的顺序.

The value of format will be adjusted with the right format based on the locale. The trick now is to scan format to see what order you find M, d, and y.

这篇关于获取用户的日期格式? (DMY,MDY,YMD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆