Question

I have a string like that:

NSString *string = @"2012-06-14T00:00:00+03:00";

I tried this and get "2012-06-14 21:00:00 GMT". But i need "14.06.2012". Is it possible?

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";

NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:string range:nil error:&error];
Was it helpful?

Solution

You'll need to create a second NSDateFormatter for the desired format (in this case "dd.MM.yyyy"), and use its stringFromDate: methods on your date object to get a string of the desired format. Try adding this code after your existing code.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy";
NSString *result = [formatter stringFromDate:date];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top