Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime / string format in IOS

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?

like image 910
PassionateDeveloper Avatar asked Dec 02 '22 00:12

PassionateDeveloper


1 Answers

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

like image 180
zaph Avatar answered Dec 05 '22 09:12

zaph