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?
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With