Datetime / string format in IOS

datetime, ios, nsstring

Solution

Example of converting one date string to another format:

NSString *currentDateString = @"04-08-2012 08:16:00";

NSLog(@"currentDateString: %@", currentDateString);

NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];

[dateFormater setDateFormat:@"MM-DD-yyyy HH:mm:ss"];
NSDate *currentDate = [dateFormater dateFromString:currentDateString];
NSLog(@"currentDate: %@", currentDate);

[dateFormater setDateFormat:@"yyyy-MM-DD HH:mm:ss"];
NSString *convertedDateString = [dateFormater stringFromDate:currentDate];
NSLog(@"convertedDateString: %@", convertedDateString);

[dateFormater setDateFormat:@"DD.MM.yyy HH:mm:DD"];
NSString *germanDateString = [dateFormater stringFromDate:currentDate];
NSLog(@"germanDateString: %@", germanDateString);

NSLog output: currentDateString: 04-08-2012 08:16:00 currentDate: 2012-04-01 12:16:00 +0000 convertedDateString: 2012-04-92 08:16:00 germanDateString: 92.04.2012 08:16:92

Problem

I have a NSString that is a date and time in this code: "YYYY-MM-DD HH:mm:SS" and I want to habe it like german style: "DD.MM.YYY HH:mm:DD" How to solve?

Original source