Nic
Nic

Reputation: 617

How to convert NSDate to NSString?

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.

Upvotes: 7

Views: 22818

Answers (1)

hallski
hallski

Reputation: 127929

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.

Upvotes: 20

Related Questions