Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 260
Jacob Avatar asked May 10 '13 11:05

Jacob


People also ask

What is locale date?

The LOCALE functions take an alphanumeric item (a character string) in the format of a date, for the LOCALE-DATE intrinsic function, or in the format of a time, for the LOCALE-TIME intrinsic function and return another alphanumeric item with the date or time formatted in a culturally appropriate way.

What date format does USA use?

The United States is one of the few countries that use “mm-dd-yyyy” as their date format–which is very very unique! The day is written first and the year last in most countries (dd-mm-yyyy) and some nations, such as Iran, Korea, and China, write the year first and the day last (yyyy-mm-dd).


1 Answers

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.

like image 98
Dave DeLong Avatar answered Sep 29 '22 03:09

Dave DeLong