-3

I'm trying to get an NSDate in a format that can be accepted by the web services of my mobile app. The app is currently outputting in the format 2013-03-26 17:27:55 on older generation generation devices (iPhone 3GS, 4 and the iOS Simulator), where the 12-hour time appears in the format 5:27 PM in the status bar. This is fine, and it's the format that I need it to be in for the server.

My problem arises when using newer devices (including the iPhone 4S and the iPhone 5) where the 12-hour time appears in the format 5:27 p.m in the status bar. On these devices, the app outputs the in the format 2013-03-26 05:27:55 PM. Here's a summary of the problem:

Web service accepts yyyy-mm-dd HH:MM:ss
iPhone 4 and earlier yyyy-mm-dd HH:MM:ss (correct)
iPhone 4s and later yyyy-mm-dd HH:MM:ss AM/PM (incorrect and rejected)

So, how can I get the outputted date on the 4S and later to be in 24-hour format when 24-hour time is OFF in the User's General Settings?

EDIT:

I'm using the following (NSDate *)getCurrentDate method to try and modify the format of the NSDate object.

NSDate *currentDateAndTime = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSLog(@"AM Symbol: %@", formatter.AMSymbol);
NSLog(@"PM Symbol: %@", formatter.PMSymbol);

NSString *dateString = [formatter stringFromDate:currentDateAndTime];
NSDate *date = [formatter dateFromString:dateString];

NSLog(@"currentDate: %@ currentDateString: %@", date, dateString);

return [formatter dateFromString:[formatter stringFromDate:currentDateAndTime]];
Martin T.
  • 445
  • 1
  • 7
  • 18
  • How are you converting NSDate to NSString in your app currently? – Anupdas Mar 26 '13 at 17:47
  • 1
    Have you tried using an NSDateFormatter? – Rich Schonthal Mar 26 '13 at 17:51
  • For specific formats like that, you really need to use an NSDateFormatter with a specified format string. For human consumption, that's frowned upon, preferring the defaults. For machine consumption, it's really necessary. – gaige Mar 26 '13 at 17:52
  • Hey guys, the date needs to be in an NSDate format. I've tried converting it to a string and mutating the string to remove am/pm, and I've tried spelling out the date format using NSDateFormatter, but I haven't had any luck. It's still coming back with that a.m. or p.m. at the end of it. – Martin T. Mar 26 '13 at 17:56
  • 2
    Could you add the code for how you're sending the `NSDate` to the web service? – Craig Siemens Mar 26 '13 at 18:07
  • 1
    There is no such thing as an 'NSDate format', it's an object that represents a time stamp. Any string formatting is done through formatting code in its description method. Which probably uses an NSDateFormatter.. – Carl Veazey Mar 26 '13 at 19:08
  • Have you seen [this question](http://stackoverflow.com/q/6613110/581994)? – Hot Licks Mar 26 '13 at 20:25
  • Hot Licks, I have tried the solution outlined in the question you have linked, but it still doesn't work. @CleverError I could add the code, but all that's happening is I'm converting the authenticationCriteria array to a JSON NSString representation. I'm using SBJSON-Framework, found here: [link](https://github.com/stig/json-framework) – Martin T. Mar 27 '13 at 14:32
  • What doesn't work? (You should edit your post to include some of the code you've tried.) – Hot Licks Mar 27 '13 at 14:34
  • What does the above log??? – Hot Licks Mar 27 '13 at 14:59
  • 2
    Remember, ***NSDate does not have a format.*** You can't change the default format it logs as. All you can do is "format" the contents of an NSDate into an NSString of your preferred format. – Hot Licks Mar 27 '13 at 15:00
  • This is a no-op: `[formatter dateFromString:[formatter stringFromDate:currentDateAndTime]]` – Hot Licks Mar 27 '13 at 15:01

1 Answers1

1

Use a date formatter

- (NSString *)dateStringForWebService:(NSDate *)date
{
    //If you call this often, you should cache the date formatter object
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];
    NSString * formattedDate = [dateFormatter stringFromDate:date];
    return formattedDate;
}
Fruity Geek
  • 7,351
  • 1
  • 32
  • 41
  • Hey, I have already tried this. My `getCurrentDate` method replaces my calls to `[NSDate date]` throughout the application. No matter what I do, I cannot get rid of the a.m. or p.m. in the NSDate object. I've edited my question above to include the `getCurrentDate` method. – Martin T. Mar 27 '13 at 14:39
  • 1
    Your code returns a NSDate object - **not a NSString object**. In an NSDate object, the date is representing by a floating point number which is the number of seconds since 1970. *It has no representation of AM or PM.* If you want a specifically formatted string based on the NSDate, you must use a NSDateFormatter. – Fruity Geek Mar 27 '13 at 19:09