0

Possible Duplicate:
NSString to NSDate
How to parse a date string into an NSDate object in iOS

I have NSString *dateAndTime = @"2011-11-22 03:20:39".

I want to get a NSString *dateWithoutTime (e.g. "2011-11-22") from this above string.

How do I get?

Community
  • 1
  • 1
Voloda2
  • 12,359
  • 18
  • 80
  • 130

2 Answers2

3

You can get the corresponding NSDate by

NSString *dateString = @"2011-11-22 03:20:39";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
NSLog(@"%@",date);

But this has been answered so many times. Search the previous posts before asking a new question.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
3

Use this

NSString *dateAndTime = @"2011-11-22 03:20:39";
NSString *date = [[dateAndTime componentsSeparatedByString:@" "] objectAtIndex:0];
beryllium
  • 29,669
  • 15
  • 106
  • 125