27

I have an NSString like this: @"3/15/2012 9:15 PM" and I would like to convert it to NSDate, I have done like this:

NSString *str =@"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"mm/dd/yyyy HH:mm"];
NSDate *date = [formatter dateFromString:];

NSLog(@"%@", date);  // date = null

Can you help me please, thanks.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
samir
  • 4,501
  • 6
  • 49
  • 76
  • possible duplicate of [convert string to nsdate](http://stackoverflow.com/questions/2311421/convert-string-to-nsdate) – jscs Mar 19 '12 at 18:09

4 Answers4

59

Use the following solution

NSString *str = @"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

formatter.dateFormat = @"MM/dd/yyyy HH:mm a";

NSDate *date = [formatter dateFromString:str];

NSLog(@"%@", date);

Edit: Sorry, the format should be as follows:

formatter.dateFormat = @"MM/dd/yyyy hh:mm a";

And the time shown will be GMT time. So if you add/subtract the timezone, it would be 9:15 PM.

Edit: #2

Use as below. You would get exact time too.

NSString *str = @"3/15/2012 9:15 PM";

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

formatter.dateFormat = @"MM/dd/yyyy hh:mm a";

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

formatter.timeZone = gmt;

NSDate *date = [formatter dateFromString:str];

NSLog(@"%@",date);
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
1

Your formatter is wrong. According to the NSDateFormatter documentation it should be "MM/dd/yyyy h:mm a". You also probably want to set the locale as stated in the Date Formatting Guideline:

If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences.

Try this:

NSString *str = @"3/15/2012 9:15 AM";
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:enUSPOSIXLocale];
[formatter setDateFormat:@"MM/dd/yyyy h:mm a"];
NSDate *date = [formatter dateFromString:str];

NSLog(@"%@",date);

Outputs:

2012-03-19 12:37:27.531 Untitled[6554:707] 2012-03-15 08:15:00 +0000
0

Hmmm - you've gone wrong for a couple of reasons

  1. the NSDateFormatter is strict and you have not been.
  2. you didn't supply str to dateFromString:
NSString* str = @"3/15/2012 9:15 PM";
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm a"];
NSDate* date = [formatter dateFromString:str];


NSLog(@"%@",date);
Damo
  • 12,840
  • 3
  • 51
  • 62
0
  NSString *str =@"3/15/2012 9:15 PM";
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
   [dateformatter setDateFormat:@"MM/dd/yyyy"];
NSArray *firstSplit = [str componentsSeparatedByString:@" "];
NSDate *dateSelected = [dateformatter dateFromString:[firstSplit objectAtIndex:0]];

    [dateformatter setDateFormat:@"mm:ss"];
NSDate *dateSelected2 = [dateformatter dateFromString:[firstSplit objectAtIndex:1]];
NSLog(@"%@",[dateformatter stringFromDate:dateSelected2]);

NSLog(@"%@",[dateformatter stringFromDate:dateSelected]);
Rama Rao
  • 1,043
  • 5
  • 22
  • whats the problem do you want the time – Rama Rao Mar 19 '12 at 11:41
  • [dateformatter setDateFormat:@"mm:ss"]; NSDate *dateSelected2 = [dateformatter dateFromString:[firstSplit objectAtIndex:1]]; NSLog(@"%@",[dateformatter stringFromDate:dateSelected2]); – Rama Rao Mar 19 '12 at 11:45