Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

ios - Date formats from device based on locale

As per my knowledge the date format will vary based on locale, Is there a way to read date-format from device's local settings for current date?

  1. Whether the date format is "dd/MM/yyyy" or "MM/dd/yyyy".
  2. Time format is "h:mm a" or something else.
  3. The AM/PM text is "AM" or "PM".

I can't afford for any third party libraries, please tell me whether this can be done using any apple classes.

Edited:

I don't want to set any formats, i just want to retrieve it from device based on locale.

say, here in my country the date format is dd/MM/yyyy in some part of the word its MM/dd/yyyy i want to get this format.

Any answer is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The method you're looking for is +[NSDateFormatter dateFormatFromTemplate:options:locale:].

This method is specifically for "localizing" a date format into a desired locale.

For example, if you want to know the user's preferred year-month-day format, you do:

NSString *fmt = [NSDateFormatter dateFormatFromTemplate:@"dMMMMy" options:0 locale:[NSLocale currentLocale]];

Or if you want to know their preferred time format, you do:

NSString *fmt = [NSDateFormatter dateFormatFromTemplate:@"jm" options:0 locale:[NSLocale currentLocale]];

You use this method when you need the actual format string. If you just want to format a date according to the predefined styles (long, full, medium, short), then you use the constants as explained in a couple of the other answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...