Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSDate to NSString?

Tags:

iphone

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.

like image 760
Nic Avatar asked Nov 21 '09 12:11

Nic


People also ask

How do I get the current date in Objective C?

dateFormat = @"MMMM dd, yyyy"; NSString* dateString = [formatter stringFromDate:date]; Convert a String to a Date: NSDateFormatter* formatter = [[NSDateFormatter alloc]init];


1 Answers

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.

like image 127
hallski Avatar answered Sep 21 '22 22:09

hallski