smartsanja
smartsanja

Reputation: 4540

Localize NSDateFormatter which has a custom string?

I want to localize NSDateFomatter, which has 'at' word in between date and time. Any idea how to do it?

//Create date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Format
[dateFormatter setDateFormat:@"MMMM d 'at' h:mm a"];
return [dateFormatter stringFromDate:self];

Upvotes: 1

Views: 380

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70946

You're making this harder than it needs to be.

If you use NSDateFormatterLongStyle for both the dateStyle and timeStyle, then NSDateFormatter will give you something like "November 30, 2015 at 3:35:32 PM MST". You can adjust that by using different built-in styles. For example if you set the timeStyle to NSDateFormatterShortStyle but keep dateStyle as NSDateFormatterLongStyle, the result is something like "November 30, 2015 at 3:36 PM". Don't use dateFormat, let NSDateFormatter figure out the localization for you.

This gives you the "at" when in the USA (for example), but NSDateFormatter will convert this into a format appropriate for the current locale. As a result you'll get something appropriate for however the locale normally represents time and date, but you don't need to tell it to use the word "at" or try to figure out how to translate it.

Upvotes: 1

Related Questions