format a NSDate to DDMMYYYY?

clement picture clement · May 5, 2011 · Viewed 8k times · Source

I have to send a DDMMYYY date from a Date to communicate with an API.

NSDateComponents *dateComponents; NSCalendar *gregorian; NSDate *date;

gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

dateComponents = [[NSDateComponents alloc] init];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];

Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP?

Answer

visakh7 picture visakh7 · May 5, 2011

Try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ddMMyyyy"];
NSString *textDate = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
NSLog(@"Date %@",textDate);
[dateFormatter release];