I've a date like this (retrieved via Twitter API) Thu Jun 05 12:57:52 +0000 2014
and I would like transform in Italian style: Giovedì 5 Giugno 2014
.
Is possible using NSDateFormatter
, if yes how ?
I've a date like this (retrieved via Twitter API) Thu Jun 05 12:57:52 +0000 2014
and I would like transform in Italian style: Giovedì 5 Giugno 2014
.
Is possible using NSDateFormatter
, if yes how ?
Yes it is, use the correct format and locale.
NSString *localeIdentifier = @"en_US";
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:@"MMMM dd yyyy"];
[dateFormatter setLocale:locale];
NSString *formattedDateString = [dateFormatter stringFromDate:[NSDate date]];
The result from this code will be June 05 2014
. As you see I am using en_US locale.
that creates the italian date for you:
NSString *_dateInString = @"Thu Jun 05 12:57:52 +0000 2014";
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
[_dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *_date = [_dateFormatter dateFromString:_dateInString];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"it_IT"]];
[_dateFormatter setDateFormat:@"EEEE d MMMM yyyy"];
NSLog(@"%@", [_dateFormatter stringFromDate:_date]);
the console says:
giovedì 5 giugno 2014
additionally, if you'd like to change the months' names or the weekdays' names (e.g. making them uppercase), you can play with the following method of the NSDateFormatter
(I don't speak any Italian, so I leave it for someone who does):
-setWeekdaySymbols:
-setShortWeekdaySymbols:
-setVeryShortWeekdaySymbols:
-setStandaloneWeekdaySymbols:
-setShortStandaloneWeekdaySymbols:
-setVeryShortStandaloneWeekdaySymbols:
-setMonthSymbols:
-setShortMonthSymbols:
-setVeryShortMonthSymbols:
-setStandaloneMonthSymbols:
-setShortStandaloneMonthSymbols:
-setVeryShortStandaloneMonthSymbols:
you can find the official documetation for those here.
Try this code snippet first i have converted given date to english date then to Italian
NSString * yourJSONString = @"Thu Jun 05 12:57:52 +0000 2014";
NSString *localeIdentifier = @"en_US";
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:@"EEEE MMMM dd HH:mm:ss Z yyyy"];
[dateFormatter setLocale:locale];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
NSLog(@"%@", dateFromString);
NSString *localeIdentifier1 = @"it_IT";
NSLocale *locale1 = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier1];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];;
[dateFormatter1 setDateFormat:@"EEEE d MMMM yyyy"];
[dateFormatter1 setLocale:locale1];
NSString *formattedDateString = [dateFormatter1 stringFromDate:dateFromString];
NSLog(@"%@", formattedDateString);