15

I have the following code:

 NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setFormatterBehavior:NSDateFormatterBehaviorDefault];

    [df dateFromString:@"2011-04-12 10:00:00"];

In which it always generates a null date. Why is this?

aherlambang
  • 14,290
  • 50
  • 150
  • 253
  • 4
    Looks like the same question you asked yesterday but simplified. You should modify the original question. Some of us spent a lot of time trying to help you. http://stackoverflow.com/questions/5628062/memory-management-and-global-variable/5628202#5628202 – XJones Apr 12 '11 at 16:25

10 Answers10

29

If the device is set to AM/PM time and requested string format is set to @"yyyy-MM-dd HH:mm:ss" dateFromString will return nil.

If you set the locale to @"en_US" conversion will return correct date.

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:@"2013-02-14 09:30:00"];
Alex
  • 2,468
  • 1
  • 20
  • 16
19

Are you doing this in a background thread? I had weird experiences with NSDateFormatter when not being used in the ui-thread. Anyway, here's the method I use, should work for you:

+ (NSDate*)parseDate:(NSString*)inStrDate format:(NSString*)inFormat {
    NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
    [dtFormatter setLocale:[NSLocale systemLocale]];
    [dtFormatter setDateFormat:inFormat];
    NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
    [dtFormatter release];
    return dateOutput;
}
Jan Gressmann
  • 5,481
  • 4
  • 32
  • 26
8

I had the same issue. I started by double-checking that the string used for conversion was valid by using this resource.

I then had a thought that it might be the locale, so I tried the following:

[dateFomatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"]]

It worked like a charm. Hope this helps.

Troy Alford
  • 26,660
  • 10
  • 64
  • 82
4

Yes, agreed with @Jan Gressmann, this is so wierd! I am using the NSDateFormatter on non-ui thread as well, and dateFromString selector returns null sometimes... For example, I have this code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormat setLocale:usLocale];
[dateFormat setDateFormat:@"dd MMM yy HH:mm:ss"];
NSDate *startDate = [dateFormat dateFromString:str_StartDate];
NSDate *endDate = [dateFormat dateFromString:str_endDate];

Output in console:

(lldb) po str_StartDate
(NSString *) $7 = 0x06da2180 03 SEP 2012 10:00:00
(lldb) po str_endDate
(NSString *) $8 = 0x06d3a810 08 SEP 2013 10:00:00
(lldb) po startDate
(NSDate *) $9 = 0x00000000 <nil>
(lldb) po endDate
(NSDate *) $10 = 0x06db05b0 2013-09-08 02:00:00 +0000

This is really wierd...

Priyal
  • 879
  • 1
  • 9
  • 30
Zennichimaro
  • 5,236
  • 6
  • 54
  • 78
4

I just ran this code and it worked for me.

NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];

NSDate *theDate = [df dateFromString:@"2011-04-12 10:00:00"];
NSLog(@"date: %@", theDate);

The output was: date: 2011-04-12 17:00:00 +0000

NWCoder
  • 5,296
  • 1
  • 26
  • 23
3

I was getting similar issue with iOS8 (XCode 6), I did following changes in NSDateFormatter -

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

[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *formattedDate = [dateFormatter dateFromString:dateString];
Muzammil
  • 1,529
  • 1
  • 15
  • 24
3

dateFromString and stringFromDate return null because of device time format (ex: 12hr Or 24hr) and device time zone

Convert 12hr To 24hr

- (NSString*)getTimeIn24Hr:(NSString*)time12hrWithDate
{
    NSDateFormatter* df12 = [[NSDateFormatter alloc] init];
    [df12 setTimeZone:[NSTimeZone systemTimeZone]];
    [df12 setDateFormat:@"yyyy-MM-dd hh:mm a"];
    NSDate* dateTime12hr = [df12 dateFromString:time12hrWithDate];

    NSDateFormatter *df24 = [[NSDateFormatter alloc]init];
    [df24 setLocale:[NSLocale systemLocale]];
    [df24 setDateFormat:@"HH:mm:ss"];
    NSString * time24HrOutput = [df24 stringFromDate:dateTime12hr];

    return time24HrOutput;
}

Convert 24hr To 12hr

- (NSString*)getTimeIn12Hr:(NSString*)time24hrWithDate
{
    NSDateFormatter* df24 = [[NSDateFormatter alloc] init];
    [df24 setLocale:[NSLocale systemLocale]];
    [df24 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate* dateTime24hr = [df24 dateFromString:time24hrWithDate];

    NSDateFormatter *df12 = [[NSDateFormatter alloc]init];
    [df12 setDateFormat:@"hh:mm a"];
    NSString * time12HrOutput = [df12 stringFromDate:dateTime24hr];

    return time12HrOutput;
}
Shrikant Tanwade
  • 1,391
  • 12
  • 21
0

Try to set the both date formatters in the same format while converting (date to string) and (string to date).In the following format

[formatter1 setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]

[formatter2 setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]
codercat
  • 22,873
  • 9
  • 61
  • 85
SURESH SANKE
  • 1,653
  • 17
  • 34
0

I have the similar issue with you, dateFromString: success in ios simulator, while in iPhone, it always return NULL, after I add the below code attached with dateFormatter, it success in iPhone ad simulator:

[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
jsvisa
  • 2,987
  • 2
  • 24
  • 30
0

You can use this format "yyyy-MM-dd hh:mm:ss".

The key is using downcased hh rather than HH to format hour when you use swift2.3 on iOS10

Hope that could be useful.

Hughes
  • 16