NSDateFormatter Issue - Getting wrong date format

Area:

Foundation


Version/Build:

iOS 11.3.1 and 11.1.2


Summary:

getting wrong DateTime Format In iPhone Device.


we using DateTime format as yyyy-MM-dd HH:mm:ss and replace empty with T

and excepted result as 2018-07-13T15:07:36, but getting date time as 2018-07-13T3:07:36TPM


Steps to Reproduce:

//Method to get DateTime String

+ (NSString *)getCurrentLocalDateTime

{

NSDate *localDate = [NSDate date];


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

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];


NSString *dateString = [dateFormatter stringFromDate:localDate];

dateString = [dateString stringByReplacingOccurrencesOfString:@" " withString:@"T"];

NSLog(@"CURR: %@", dateString);


return dateString; // yyyy-MM-ddTHH:mm:ss

}


Expected Results:

Output Data must be - 2018-07-13T15:07:36


Actual Results:

Actual Data - 2018-07-13T3:07:36TPM


Kindly reply ASAP..

If you are targetting iOS 10.0 or later, just use NSISO8601DateFormatter:


+ (NSString *)currentLocalDateTime {
    NSDate *localDate = [NSDate date];
    NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
    NSString *dateString = [formatter stringFromDate:localDate];
    NSLog(@"%@", dateString);
    return dateString;
}


Output:


2018-07-17T19:20:48Z

When you want some specific format, you may need to set locale to "en_US_POSIX".

One more, replacing the result later does not seem to be a good way.


+ (NSString *)getCurrentLocalDateTime
{
    NSDate *localDate = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; // <- include 'T' in your format
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]]; // <- set locale "en_US_POSIX"
    
    NSString *dateString = [dateFormatter stringFromDate:localDate];
    NSLog(@"CURR: %@", dateString);
    
    return dateString; // yyyy-MM-ddTHH:mm:ss
}


I have not tested with all possible settings, but I get 2018-07-18T05:31:18 as the output.

From: https://developer.apple.com/library/archive/qa/qa1480/_index.html



On iOS, the user can override the default AM/PM versus 24-hour time setting (via Settings > General > Date & Time > 24-Hour Time), which causes NSDateFormatter to rewrite the format string you set, which can cause your time parsing to fail.

To solve this problem properly, you have to understand that NSDateFormatter has two common roles:

  • generating and parsing user-visible dates
  • generating and parsing fixed-format dates, such as the RFC 3339-style dates used by many Internet protocols


If you're working with user-visible dates, you should avoid setting a fixed date format string because it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should limit yourself to setting date and time styles (via

-[NSDateFormatter setDateStyle:]
and
-[NSDateFormatter setTimeStyle:]
) or generate your date format string from a template (using
+[NSDateFormatter dateFormatFromTemplate:options:locale:]
).

On the other hand, if you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences. "en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iOS as it does on OS X, and as it it does on other platforms).

Once you've set "en_US_POSIX" as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.

Posted in Bug Reporter and got reply from Apple..


Apple Developer Relations

July 18 2018, 1:06 AM

This issue behaves as intended based on the following:
This is the correct result.
First,
you don't need to post-process the string, and generally shouldn't.
You can specify that the separator between date and time
should be a literal 'T' with the format string @"yyyy-MM-dd'T'HH:mm:ss".
Second,
NSDateFormatter will adjust the format string to match the user's locale preferences.
This is an intentional feature.
If you need an ISO 8601 representation of the date,
you can use NSISO8601DateFormatter, which
(1) always uses the 'T' separator between date and time, and
(2) does not adjust its format to match user preferences for localized dates.
If you can't use NSISO8601DateFormatter because you need to
support iOS versions from before it was introduced,
you can set your NSDateFormatter's locale to the en_US_POSIX locale, which does no localization.
One additional note:
Creating formatters (including NSDateFormatters and NSISO8601DateFormatters) is expensive.
You probably should reuse this formatter
if you're going to format many dates with this method.
Please update your bug report to let us know if this is still an issue for you.


https://pasteboard.co/Hv7vf0J.png

Looks like Apple gave you the same advice we have given you in this thread. 🙂

Why Apple is not giving the Date & Time option in Emulator, making it so hard to debug.

Why Apple is not giving the Date & Time option in Emulator

First up, I’m presuming you mean the simulator here. That terminology is important; the iOS Simulator is not an emulator, and if you go into this thinking it is then you’ll end up very confused.

Having said that:

  • The iOS Simulator does support changing date and time settings, via Settings > General > Language & Region.

  • You can also set this up in your scheme, via the App Region popup in the Options tab of the scheme editor.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NSDateFormatter Issue - Getting wrong date format
 
 
Q