55

I have made a calendar application for the iPhone in which I have a date in string format (e.g. "Tue, 25 May 2010 12:53:58 +0000").

I want to convert this to an NSDate.

What do I need to use to do that?

jscs
  • 63,694
  • 13
  • 151
  • 195
AppAspect
  • 4,461
  • 2
  • 30
  • 46

4 Answers4

129

Take a look at the class reference for NSDateFormatter. You use it like this:

NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr]; 
[dateFormat release];

For more information on how to customize that NSDateFormatter, try this reference guide.

EDIT:

Just so you know, this is going to parse the full month name. If you want three letter month names, use LLL instead of LLLL.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sam Ritchie
  • 10,988
  • 4
  • 42
  • 53
  • 6
    That reference guide link is dead. – KClough Feb 14 '12 at 15:00
  • @AppAspect I know this is 800 years later, but try @"LLLL d, HH:mm Z" OR @"MMM dd hh:mm a". Reference: http://unicode.org/reports/tr35/tr35-10.html#Date_Field_Symbol_Table – migs647 May 09 '13 at 21:11
  • According to this: http://www.unicode.org/reports/tr35/ three L's gets you 4 letter month names, not 3... But it seems to work anyway. Was crashing before, now it isn't... Maybe something about the breakpoint I had there before. – shim Sep 12 '13 at 22:32
  • is there a way to this in a generic way? what if the date format in a service you have no control over changes? – Carlo Dec 17 '13 at 22:27
  • not working in swift :( see this https://stackoverflow.com/questions/45399063/ios-swift-3-converting-string-to-date – Abhishek Thapliyal Jul 30 '17 at 11:00
8
-(NSString *)dateToFormatedDate:(NSString *)dateStr {
    NSString *finalDate = @"2014-10-15";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    [dateFormatter setDateFormat:@"EE, d MMM, YYYY"];
    return [dateFormatter stringFromDate:date];
}
nicosantangelo
  • 13,216
  • 3
  • 33
  • 47
Anand Prakash
  • 313
  • 5
  • 20
6

If you are storing dates in one of iOS' styles, it's far easier and less error prone to use this method:

// Define a date formatter for storage, full style for more flexibility
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];

// Use today as an example
NSDate *today = [NSDate date];
// Format to a string using predefined styles
NSString *dateString = [dateFormatter stringFromDate:today];
// Format back to a date using the same styles
NSDate *todayFromString = [dateFormatter dateFromString:dateString];
Pier-Luc Gendreau
  • 13,553
  • 4
  • 58
  • 69
-1
NSString *dateString=@"2017-05-25";

NSDateFormatter *dateFormatter=[NSDateFormatter alloc]init];

[dateFormatter setDateFormatter:@"MM-dd-yyyy"];

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

date=[datFormatter dateFromString:dateString];
Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
Shaik Tamim
  • 95
  • 12
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Nahuel Ianni May 03 '17 at 09:15