masgar masgar - 2 months ago 8
iOS Question

NSDateformatter setDateFormat according to currentLocale

I'm going mad with, probably, a stupid problem.

I have 3 strings: year, month and day. I need to have a date in the right format based on currentLocale, so i.e. if currentLocale localeIdentifier is en_US my dateFormat should be:
MMM/dd/yyyy

if it's fr_FR the dateFormat should be dd/MMM/yyyy

I don't think the only way to do this is to get currentLocale localeIdentifier and start with a bunch of if then.

Thanks in advance.

Max

Answer

Look at NSDateComponents to create an NSDate, then use NSDateFormatter to format it. NSDateFormatter uses the current locale to format dates, based on the format style (e.g.
NSDateFormatterMediumStyle).

Comments