0

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 ?

Fry
  • 6,235
  • 8
  • 54
  • 93

3 Answers3

0

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.

Date Format Patterns

  • You are using `[NSDate date]` but as I wrote, I have a string not a date. A string like this `Thu Jun 05 12:57:52 +0000 2014`. – Fry Jun 05 '14 at 13:34
  • You can reverse the date string to date using NSDateFormatter and correct date format. Check this http://stackoverflow.com/questions/18098647/ios-how-to-set-date-format – George Petrov Jun 05 '14 at 13:35
0

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.

holex
  • 23,961
  • 7
  • 62
  • 76
  • I have saved the result from console directly after I ran the code. your problem is, my little friend, you have to set the locale from Italian to English. otherwise, the weeks' or months' names cannot be parsed, because they are written in English not Italian. – holex Jun 05 '14 at 15:47
0

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);
Agent Chocks.
  • 1,312
  • 8
  • 19
  • why have not you parsed the timezone properly? – holex Jun 05 '14 at 15:45
  • i have used this "EEEE dd MMMM yyyy" as you want the output as "Giovedì 5 Giugno 2014" and here "dd" is used as date can be in two digits (e.g, 25) – Agent Chocks. Jun 06 '14 at 05:23
  • the formatter `dd` forces to show two digits like in case of `5` it will be `05` but single `d` always shows the _valuable_ digits only without leading zeros, so in case of `25` the output will be `25`. overall, the `25` will appear properly with formatter `d` but OP did not request any leading zero for the output. – holex Jun 06 '14 at 07:58
  • i have not checked with single "d" but not sure the "25" will be properly displayed using single "d", will check this and revert back – Agent Chocks. Jun 06 '14 at 13:26
  • maybe reading the _Unicode Technical Standards #35_ could help to understand how those formatters work, here is the link for it: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns – holex Jun 06 '14 at 15:04