Converting NSDate to NSString

suggest change

If we have an NSDate and want to convert it to an NSString, use an NSDateFormatter. There are many date-string formats. Three steps:

  1. Create an NSDateFormatter object.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1. Set the date format you want in the string.
dateFormatter.dateFormat = @"yyyy-MM-dd 'at' HH:mm";
  1. Get the formatted string.
NSDate *date = [NSDate date]; // your NSDate object
NSString *dateString = [dateFormatter stringFromDate:date];

This will give output something like this: 2001-01-02 at 13:00

Note:

Creating an NSDateFormatter instance is an expensive operation, so it is recommended to create it once and reuse when possible.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents