7

I want to convert NSDate to NSString? How's that possible?

I tried this, but it didn't work (it is generating Exception) :

   NSString *dateToString=[[NSString alloc] initWithFormat:(NSString *)dateObj];

Thanks in advance.

rohan-patel
  • 5,761
  • 5
  • 42
  • 68
Nic
  • 601
  • 4
  • 12
  • 20

1 Answers1

20

You can achieve this with the NSDateFormatter.

Example:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

NSString *string = [dateFormatter stringFromDate:date];
[dateFormatter release];

For more ways of controlling the format of the string, see the reference documentation for NSDateFormatter.

Graham
  • 7,035
  • 17
  • 57
  • 82
hallski
  • 116,451
  • 4
  • 31
  • 21