anemo
anemo

Reputation: 1367

iOS - Get local date & time in International format and English language

I am trying to get the user's local Date & Time using the below code

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"dd MMM yyyy"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat: @"HH:mm"];
[timeFormatter setTimeZone:[NSTimeZone localTimeZone]];

self.appearedOnDate = [dateFormatter stringFromDate:[NSDate date]];
self.appearedOnTime = [timeFormatter stringFromDate:[NSDate date]];

It returns correct values, but in the language set on users device. For example, in a case where user has Simplified Chinese set on his device, I am getting the output as

24 3月 2015 --- 22:13

How can I get the local time in English language ?

I guess this happens only in iOS8 and not on iOS6, but I am not sure.

Upvotes: 0

Views: 583

Answers (1)

casillas
casillas

Reputation: 16793

You might add the following code before print your output.

 [NSLocale availableLocaleIdentifiers];
 NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
 [timeFormatter setLocale:locale];

Upvotes: 2

Related Questions