Reformat NSString representing date

How can I convert @ "20090302" to @ "2009/03/02" or "02/03/2009"?

I found a solution and this update

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

[formatter setDateFormat:@"YYYYMMDD"];



NSDate *date = [formatter dateFromString:yourinoutstring];


[formatter setDateFormat:@"DD/MM/YYYY"];

NSString *outDate = [formatter stringFromDate:date];
+5
source share
1 answer

For this purpose you will want to use NSDateFormatter .

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

[formatter setDateFormat:@"YourDateInputFormat"];

Where you can find out what @"YourDateInputFormat"should be by reading the Data Formatting Guide, Section> Date Formats . You get NSDatefrom this:

NSDate *date = [formatter dateFromString:yourString];

Then change the formatting format to display the desired result:

[formatter setDateFormat:@"YourDateOutputFormat];

Then you can convert this date and get the desired line:

NSString *outString = [formatter stringFromDate:date];
+7

All Articles