Search code examples
iosnsdatenscalendarnsdatecomponents

format a NSDate to DDMMYYYY?


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?


Solution

  • Try this

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