Convert english style date in italian style date - ios

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.
Date Format Patterns

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);

Related

iOS Date format 28 Mar 2016 / 16:54pm

I want to display the date in the format "28 Mar 2016 / 16:54pm" from the string "2017-05-05T08:27:31.337Z". How can I do this? Is there any buildin method for iOS? Thanks.
use NSDateFormatter for your concept
for e.g
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
NSString *final = [dateFormatter stringFromDate:msgDate];
update
add setAMSymbol and setPMSymbol in your dateFormatter.
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
Full Answer
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
NSString *final = [dateFormatter stringFromDate:msgDate];
See Instruction or introduction with every line
NSString *str = #"your date here..."; /// here this is your date with format yyyyy-MM-dd'T'HH:mm:ss.SSSZ
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);

IST time zone not getting parsed

I have a string Tue Feb 23 10:12:48 IST 2014, I want to get the NSdate object form this. I am using below code snippet
NSString * buildTime = #"Tue Feb 23 10:12:48 IST 2014";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSDate *date = [formatter dateFromString:buildTime ];
Every time I am getting nil value to date variable. If I am changing IST to GMT in build time then NSDate is perfectly coming. Can any one correct me where I am doing mistake in this.
Use small 'zzz' instead of 'ZZZ',
NSString * buildTime = #"Tue Feb 23 10:12:48 IST 2014";
NSLocale *indianEnglishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:indianEnglishLocale];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSDate *date = [dateFormatter dateFromString:buildTime];
NSLog(#"date %#",date);
NSLog(#"string %#",[dateFormatter stringFromDate:date]);

Convert NSString to NSDate and back to different format NSString

I need to take the NSString Dec 4, 2012, 12:33 PM and convert it to separate out the month, day, and year, so that I can have 3 different strings of 12, 04, and 2012.
I figure that I should convert the NSString to NSDate and then reformat the date to change out NSString, but am running into issues.
I have:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:substring];
NSLog(#"Date%#", dateFromString);
[dateFormatter release];
However, the date keeps coming back null.
The problem is with your locale I think.
You should try to print how your dateFormatter formats current date [NSDate new].
It works for me:
NSString* substring = #"Dec 12 2012 12:08 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d yyyy h:mm a"]; // not 'p' but 'a'
NSDate *dateFromString = [dateFormatter dateFromString:substring];
Convert
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
to, Because MMM dd, yyyy, hh:mm p not a valid date formate
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm a"];
And
NSDate *dateFromString = [dateFormatter dateFromString:substring];
To get string again
NSLog(#"%#",[dateFormatter stringFromDate:dateFromString]);
for me NSLog is Dec 04, 2012, 12:33 PM
#import "NSString+Date.h"
#implementation NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
{
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
return dateFromString;
}
+(NSString*)StringFromDate :(NSDate*)date;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
return stringDate;
}
` #end

NSString to NSDate using NSDateFormatter returns null

dateFromString returns me null when I am trying to convert NSString to NSDate
here is my code:
NSString *dateString = #"Wed 16 Oct 2013";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale systemLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy"];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *myDate = [dateFormatter dateFromString: dateString];
Please, give me any suggestions about this.
It's a locale issue and it's sort of the same of NSDateFormatter Strings in iOS 7, just the other way around.
Probably in your system locale Wed or/and Oct are not valid valid literals for a weekday or/and a month.
Try setting the locale to en_US or to currentLocale
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
or
NSLocale *locale = [NSLocale currentLocale];
For more formatting style http://cybersam.com/ios-dev/quick-guide-to-ios-dateformatting
NSString *dateString = #"Wed 16 Oct 2013";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"EEE dd MMM yyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#", dateFromString);

How to change date format on ios?

I have a date like this : Mon, 04 Jun 2012 14:13:00 GMT
and want to delete the time displayed so that I can have something like this : Mon, 04 Jun 2012
I tried the code below :
NSString *dateTex = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
NSLog(#"the object value is:%#",dateTex);
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"EEE, dd MMM yyyy"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
The date is parsed from an xml file and saved in the NSString dateTex. Then I am trying to change the format and save the new date on NSString dateText. However when I try to print the dateText variable, I get the object value is (null).
Any ideas?
You can try below code :
NSString *dateTex = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
NSLog(#"the object value is:%#",dateTex);
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy"];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
NSString *my_date_string = [dateFormatter stringFromDate:date];
//my_date_string returns like value Tue 05 jun 2012
Welcome!

Resources