Conversion of NSString to NSDate and NSDate to NSString

C

 

Here is the way to convert NSDate to NSString in objective C

Conversion of NSDate to NSString :

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@”yyyy-dd-MMM”];

NSString *dateString = [dateFormat stringFromDate:today];

NSLog(@”date: %@”, dateString);

[dateFormat release];

Note that today should be your NSDate object which you want to convert to NSString.

Conversion from NSString to NSDate :

NSString *dateString = @”01-02-2010″;

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

[dateFormatter setDateFormat:@”dd-MM-yyyy”];

NSDate *dateFromString = [[NSDate alloc] init];

dateFromString = [dateFormatter dateFromString:dateString];

[dateFormatter release];

You can set the date format which you want.

So simple…..!!!!!!! Enjoy coding…….!!!!!

 

 

 

 

About the author

minakshi.bhosale
By minakshi.bhosale

Category