4

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
clement
  • 4,204
  • 10
  • 65
  • 133

6 Answers6

14

Try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ddMMyyyy"];
NSString *textDate = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
NSLog(@"Date %@",textDate);
[dateFormatter release];
visakh7
  • 26,380
  • 8
  • 55
  • 69
  • There is missing semi-colon ; at the end of the NSLog statement, but otherwise works perfect! – jsherk Dec 12 '12 at 00:00
1

Just:

  NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
  [outputFormatter setDateFormat:@"ddMMyyyy"];
  NSString *textDate = [NSString stringWithFormat:@"%@",[outputFormatter stringFromDate:[NSDate date]]];
  [outputFormatter release];
Nick Forge
  • 21,344
  • 7
  • 55
  • 78
Ruben Marin
  • 1,639
  • 14
  • 24
1

Try this code:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"ddMMyyyy"];
NSDate *current = [NSDate date];
NSString *currentstring = [dateFormatter stringFromDate:current]
Aman Aggarwal
  • 3,754
  • 1
  • 19
  • 26
1
    NSDate *date=[NSDate date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"ddMMyyyy"];
    NSString *dateOfGame =[formatter stringFromDate:date];
    [formatter release];
Gypsa
  • 11,230
  • 6
  • 44
  • 82
0

Try this:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

[dateFormatter setDateFormat:@"ddMMyyyy"];

NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
0

Agree With Ruben Esposito

Just change your code like this.

NSDateFormatter *dtFormatter = [[NSDateFormatter alloc]init];
[dtFormatter setDateFormat:@"ddMMyyyy"];    
NSString *strDate = [NSString stringWithFormat:@"%@",[dtFormatter stringFromDate:[NSDate date]]];
[dtFormatter release];
NSLog(@"%@",strDate);
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94