-3

I have a string containing: 2013-10-29 18:50:18 +0000

How can i convert this into a date, but keeping the same date format?

I've tried:

        NSString *str3 = [[NSString alloc] initWithFormat:@"%@", time];

        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
        NSDate *myDate = [df dateFromString:str3];

But myDate returns (null)

Any ideas?

Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • Not duplicate because i dont understand how to convert it into the same format – Alosyius Oct 29 '13 at 19:10
  • 2
    You have to make sure your string is coming in exactly like you are stating it is: 2013-10-29 12:10:00 AM – logixologist Oct 29 '13 at 19:10
  • With standard formats like this you should consider using standard C library function like strptime_l and strftime_l. You can find an example on the end of this page: https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html – Laszlo Oct 30 '13 at 10:12
  • Why is this marked as duplicate? He is asking about **this specific date format**, not generaly _How to convert date_. – Tricertops Oct 30 '13 at 11:29

1 Answers1

4

Try this format:

yyyy-MM-dd HH:mm:ss ZZZ
  • HH for hours, because you clearly have 24-hour clock
  • ZZZ for timezone, because you used a and that’s for AM/PM period

Full reference of Unicode Date Format Patterns.

Tricertops
  • 8,492
  • 1
  • 39
  • 41