0

I'm having a hard time trying to convert a NSString to NSDate. I try lots of date format convinations, but no worked.

My string is: 5/17/2013 12:00:00 AM

And I'm using:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"M/d/y hh:mm:ss a"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *cI = [dateFormat dateFromString:@"5/17/2013 2:00:00 AM"];
[dateFormat release];

I also try with:

  • M/d/y h:mm:ss a
  • M/d/y hh:mm:ss
  • M/d/y h:mm:ss

But always cI is a nil.

Tony
  • 287
  • 5
  • 19
  • When/where do you log "cI". Are you NSLogging it immediately after the dateFromString, or somewhere else? – Hot Licks May 29 '13 at 18:51
  • Once again, where are you logging "cl"??? How do you know it's nil right after the dateFromString operation?? – Hot Licks May 30 '13 at 00:27

2 Answers2

1

Use:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *cI = [dateFormat dateFromString:@"5/17/2013 2:00:00 AM"];
[dateFormat release];

Output: 2013-05-17 02:00:00 +0000

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

here, it works fine on my locale @"MM/dd/YYYY h:mm:ss a"

Mike
  • 481
  • 10
  • 24