1

Im working on iphone app using xcode,objective c and targeting ios 5 minimum. All I am trying to do is convert a string to a date. I have read lots on this and it should be a simple straight forward task. I have seen many other questions like this in forum but what is working for people doesnt seem to be working for me. Here is what I am doing

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dobText = [dict valueForKey:@"DateOfBirth"];
NSDate *dobDate = [dateFormatter dateFromString:dobText];

the dobText is always in format @"1999-01-01" which matches the date format set in the date formatter but the result when using date from string is always nil.

can anyone explain this to me and let me know how to fix it?

glogic
  • 4,042
  • 3
  • 28
  • 44
  • 1
    The code should work. Do you get the same if you set dobText manually (dobText = @"1999-01-01";) – vakio Jul 23 '12 at 11:33
  • yeah even when manually entered into dobText or even into [dateFormatter dateFromString:@"1999-01-01"]; – glogic Jul 23 '12 at 11:34
  • it looks ok, but there has been reported some issues if you use the NSDateFormatter in a background thread. Are you doing that? http://stackoverflow.com/questions/5638416/datefromstring-always-returns-null-with-dateformatter – AndersK Jul 23 '12 at 11:44
  • 1
    which result is `nil`? the `dobText` or the `dobDate`? – holex Jul 23 '12 at 11:57
  • @glogic Can you put there a `NSLog` printing `dobText`? – Sulthan Jul 23 '12 at 14:41

4 Answers4

0

are you sure the date is using a - and not a in the data you get from the dict. As i can reproduce a nil result when i use a (alt + -)

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dobText = @"2009-02-12";
    NSDate *dobDate = [dateFormatter dateFromString:dobText];
    NSLog(@"%@", dobDate);  

Try doing this, will likely fix your problem.

 NSString *dobText = [[dict valueForKey:@"DateOfBirth"] stringByReplacingOccurrencesOfString:@"—" withString:@"-"];
bigkm
  • 2,218
  • 1
  • 16
  • 12
0

Look at the user preferences on your device. The documentation says:

Note that although setting a format string (setDateFormat:) in principle specifies an exact format, in practice it may nevertheless also be overridden by a user’s preferences—see Data Formatting Guide for more details.

Mike
  • 2,065
  • 25
  • 29
0

I suspect like bigkm say, your dashes may be getting in the way.

I would suggest you try a dummy string in line first. e.g.

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dobText = @"1999-01-01" // [dict valueForKey:@"DateOfBirth"];
NSDate *dobDate = [dateFormatter dateFromString:dobText];

Now does that work? It should. Now triangulate another way:

  1. Remove the dashes completely from the dob text, so that it has the format 'yyyyMMdd'
  2. Have the date formatter look for this same format

Does that work? It should. And that would prove that the separator characters in your format are the issue and need some further inspection (or cleansing as bigkm suggested).

Side node re threading: NSDateFormatter is fine if you use it on the thread on which it was created. If you don't, you'll know b/c the app will crash.

idStar
  • 10,674
  • 9
  • 54
  • 57
0

If your date format is correct, I can suggest to set NSLocale to your NSDateFormatter.

For me this code works on simulator:

NSString *format = [NSString stringWithFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:format];
NSDate *date = [df dateFromString:pubDateSttring];

but on the device date is always NIL.

I've found workaround of setting NSLocale:

df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
Krivoblotsky
  • 1,492
  • 11
  • 17