views:

369

answers:

3

Hi guys,

I've been stumped by the code below. I pass a string date to a UIDatePicker which seems to work on most devices, yet on some, it crashes the app.

here's the code

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
[timePicker setDate:date];
[dateFormat release];

I have a function which passes the "dateStr" variable, as follows, dateStr = @"0:0"; I cannot see what's wrong - as it works for me! (iphone 3.1.3) but it doesnt work for my tester (ipod touch 3.1.3)

Can anyone see anything wrong with the code?

Thanks v much. any ideas?

+1  A: 

Probably locale problem.

Try to set a common locale for the date formatter e.g.

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormat setLocale:usLocale];
[usLocale release];
KennyTM
this seems to have solved it. Thank you all for the answers.
Matt Facer
A: 

You set your dateFormat to

[dateFormat setDateFormat:@"HH:mm"];

yet you pass it dateStr as dateStr = @"0:0";?

Try passing in dateStr = @"00:00"; which would actually conform to your formatter's format

aSquared
A: 

All 12-hour locales define midnight as 12:00. The 24-hour (also known as military time) clock has 00:00 defined as midnight.

In your example you are "forcing" a string with an potentially unknown format on your UIDatePicker.

Check your locale settings and always specify the locale that goes with the string time representation, en_US, da_DK, et cetera.

Niels Castle