0

I have the following method in a NSDate category; it's meant to just give me the instance of the date in 24-hour format:

-(NSString *)serverHourFormat
{

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

     NSLog(@"hour before: %@",[self description]);

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

     NSLog(@"hour after: %@",[dateFormat stringFromDate:self]);

     return [dateFormat stringFromDate:self];

}

The log of before and after:

hour before: 2014-05-20 08:25:00 p.m. +0000

hour after: 03:55 p.m.

Any clue as to what I'm doing wrong here?

jscs
  • 63,694
  • 13
  • 151
  • 195
David Homes
  • 2,725
  • 8
  • 33
  • 53
  • The "hour before" is showing you the `description`, which is the time in GMT/UTC. The "hour after" is showing it to you in your current timezone. Do you want your string representation to be in GMT/UTC or the device's timezone? – Rob May 20 '14 at 20:37
  • Hi Rob, i'm aware of the description for the BEFORE, and the one after doesn't really matter for the way we are handling it (it's calculated in the server based on some coordinates we are sending). Regardless of whether the hour is right or not I do need a string with a 24 HOUR format. So in hour after it should just read "hour after: 12:55" – David Homes May 20 '14 at 20:41
  • Also, you've got your phone's 12/24 setting set in contradiction of your locale's 12/24 default. http://stackoverflow.com/a/6735644/581994 – Hot Licks May 20 '14 at 20:54
  • 1
    @JoshCaswell - He certainly can be getting those results, if he's testing on a phone and not on the simulator. – Hot Licks May 20 '14 at 20:55
  • 1
    @JoshCaswell - No, NSDateFormatter changes how it interprets date format strings. http://stackoverflow.com/questions/143075/nsdateformatter-am-i-doing-something-wrong-or-is-this-a-bug – Hot Licks May 20 '14 at 20:57
  • O_o /me boggles, @HotLicks. Thanks for the info. – jscs May 20 '14 at 20:58
  • So this might not be closed as the correct duplicate, then. @dhomes, please be more specific about the problem you have with the formatted string. – jscs May 20 '14 at 21:01
  • @dhomes Sorry, I thought you were focusing on the timezone issue. If you set the locale of the formatter (e.g. `[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]]`), this strange am/pm behavior goes away. – Rob May 20 '14 at 21:03
  • 1
    By the way, this behavior, which I consider a bug, is documented in the [Date Formatters](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1) section of the _Data Formatting Guide_, which points out that if you use `HH:mm`, that "The representation of the time may be `13:00`. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be `1:00 pm`." You can manifest this by selecting UK with 24-hour switch turned off in Settings. – Rob May 20 '14 at 21:09
  • It's what old programmers call a "feechure" -- a bug that's been concealed by documenting it so it looks like it's supposed to be that way. – Hot Licks May 20 '14 at 21:50
  • @Rob, the Locale did fix the issue, too bad the question was closed as a duplicate and you couldn't add it as an answer, I would have marked as the correct answer – David Homes May 20 '14 at 22:10
  • 1
    summing up: [dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]] fixed the issue for me. All we needed was to send an hour string to the server in 24 hours format regardless of the location or user preference, it's all computed on the server for a location other than the device based on additional information (the coordinates of a user selected location that can be anywhere in the world) – David Homes May 20 '14 at 22:12

0 Answers0