views:

1485

answers:

3

I'm having a problem. I get incoming time strings in 12-hour format, and I'm turning them into NSDate objects. When the iPhone is in 12 hour format, no problem. But when it's in 24 Hour format, things go wrong. Here's some sample code to demonstrate:

NSString *theTime = @"3:19 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"]; // "3:19 PM"
NSDate *date = [formatter dateFromString:theTime];
NSString *theString = [formatter stringFromDate:date];

In 24 hour mode, date is 1970-01-01 03:19:00, and theString is "3:19" - WRONG

In 12 hour mode, date is 1970-01-01 15:19:00, and theString is "3:19 PM" - RIGHT

So... question 1: why is the device's 24 hour setting overriding my date formatter setting?

and more importantly, question 2: How do I get a proper conversion from 12 hour time to 24 hour time?

I already have code to detect if the phone is in 24 hour mode, but other than digging around in the string and swapping the 3 with a 15, there doesn't seem to be a clean way to do this.

A: 

Hi

I believe the @"h:mm a" should be @"HH:mm a".

If you use the pre-build dateformatter in cocoa, everything will be taken care of for you.

NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];

NSDateFormatterShortStyle and NSDateFormatterNoStyle comes in different varieties. Using those will make sure you respect the settings the user has selected for dates and times.

The 12-14 hour clock conversion is taken care of by the SDK, if you have a model or some value object for storing your dates try to keep them as NSDate. This way you can format them only when you need to display them. Saving dates as strings could open a world of trouble when you maybe parse them from xml where the GMT is specified separately or try to add and subtract NSTimeIntervals.

RickiG
Thanks. I gave it a whirl with this code:NSString *theTime = @"3:19 PM";NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];[timeFormatter setTimeStyle:NSDateFormatterShortStyle];[timeFormatter setDateStyle:NSDateFormatterNoStyle];NSDate *date = [timeFormatter dateFromString:theTime];NSString *theString = [timeFormatter stringFromDate:date];And date comes up nil. I ran into this earlier when I tried this route, and it's not working. Very frustrating.
Silromen
A: 

Okay, I left a comment, but it squished all the code together, so I'll have to "answer" my question with a comment:

Thanks. I gave it a whirl with this code:

NSString *theTime = @"3:19 PM"; 
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];       
[timeFormatter setTimeStyle:NSDateFormatterShortStyle]; 
[timeFormatter setDateStyle:NSDateFormatterNoStyle]; 
NSDate *date = [timeFormatter dateFromString:theTime]; 
NSString *theString = [timeFormatter stringFromDate:date]; 

And date comes up nil. I ran into this earlier when I tried this route, and it's not working. Very frustrating.

Silromen
Hi Silromen. That approach is only for displaying dates and times. To build dates from strings you need to apply the correct format. As you do here: [formatter setDateFormat:@"h:mm a"]; // "3:19 PM" But notice the HH instead of hh.
RickiG
Thanks for the response, but that just doesn't work. On the iPhone in 24 hour mode, 3:54 PM still comes out as a 03:54:00 date object, and in the simulator (with my computer clock in 24hour mode), it was worse: 12:54:00.
Silromen
A: 

Not sure if you still need it, but I've had a similar problem which got solved by setting the locale for the date formatter. That is, if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.

Some more on that here:

http://developer.apple.com/iphone/library/qa/qa2010/qa1480.html

SVD