16

I have a string "2012-09-16 23:59:59 JST" I want to convert this date string into NSDate.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: @"2012-09-16 23:59:59 JST"];
NSLog(@"%@", capturedStartDate);

But it is not working. Its giving null value. Please help..

3 Answers 3

40

When using 24 hour time, the hours specifier needs to be a capital H like this:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

Check here for the correct specifiers : http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

However, you need to set the locale for the date formatter:

// Set the locale as needed in the formatter (this example uses Japanese)
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]];

Full working code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]];
NSDate *capturedStartDate = [dateFormatter dateFromString: @"2012-09-16 23:59:59 JST"];
NSLog(@"Captured Date %@", [capturedStartDate description]);

Outputs (In GMT):

Captured Date 2012-09-16 14:59:59 +0000
3
  • hey @danielbeard , as u said it will display the time GMT, which will show time difference of 9 hours from the exact date. How to get the exact date from the string.. ??..
    – NiKKi
    Sep 17, 2012 at 5:48
  • Dates themselves don't have timezones, but you can get a string representation in a time zone with the stringFromDate with the NSDateFormatter set up above. Sep 17, 2012 at 5:53
  • See here for more details stackoverflow.com/questions/1862905/nsdate-convert-date-to-gmt Sep 17, 2012 at 5:55
6
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: @"2012-09-16 23:59:59 GMT-08:00"];
NSLog(@"%@", capturedStartDate);
2
  • 1
    This might be useful to someone someday: Keen.io timestamp format [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; I did not need to set the locale. Sep 25, 2015 at 18:55
  • The answer was useful to me today ;)
    – Tom Howard
    Feb 3, 2016 at 16:10
1
NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful

[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
 // end
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.