Piyush
Piyush

Reputation: 1544

How to change the date formate of objects of NSMutablearray which contains dates?

I have one NSMutableArray which contain dates

for example :

 NSMutableArray *arrDates = [[NSMutableArray alloc]initWithObjects:@"22/07/2042","05/04/2015","22/08/2015","22/08/2015","22/09/2015","22/09/2015", nil];

I want to change the formate of that date in MM/dd/yyyy formate which is in dd/MM/yyyy formate. and after changing the formate i want to store in another NSMutableArray. How can I do that??

Upvotes: 0

Views: 102

Answers (1)

johnpatrickmorgan
johnpatrickmorgan

Reputation: 2372

My advice is to keep a single array of NSDate objects. NSDate represents a date irrespective of format. To obtain a formatted NSString from an NSDate, you can use NSDateFormatter.

NSDate *date = self.dates[index];
self.dateFormatter.dateFormat = @"dd-MM-yy";
NSString *dateString = [self.dateFormatter stringFromDate:date];

NSDateFormatter can also convert from NSString to NSDate. So when you parse the strings from your web service, you should convert them to NSDate objects for storage:

NSString *dateString = stringFromServer;
self.dateFormatter.dateFormat = @"dd-MM-yy";
NSDate *date = [self.dateFormatter dateFromString:dateString];

Upvotes: 1

Related Questions