NSDateFormatter- dateFromString returning nil when set AM/PM
As @rmaddy said this code works fine.
formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
[formater setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[formater setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
iOS - NSDateFormatter dateFromString returns nil only in one case
After a few comments, it has been determined that the code in the question is being run with the locale of es_PE
. This is the country of Peru. The date in question is January 1, 1990. By default, NSDateFormatter
uses the local timezone and when parsing date strings that have no time, midnight is assumed.
In Peru, in the year 1990, day light savings began at midnight, January 1st, 1990. This means that clocks went from December 31, 1989 at 11:59:59pm straight to January 1, 1990 at 1:00:00am. There was no midnight on January 1, 1990.
This is why the attempt to convert the string 01-01-1990
failed for this user. There was no midnight for this date in Peru (and possibly a few other locales, if any, that had day light saving start at the same time). Most people testing this code would claim it works just fine since most people testing this code don't live in Peru.
I found a useful website with helpful information. See http://www.timeanddate.com/time/change/peru/lima?year=1990 for details about Peru and day light savings time. Note that in 1989 and 1991, Peru did not use day light savings time.
iOS 11.2.6 DateFormatter.date returns nil for cities that observe Brasília Summer Time
The reason it doesn't work for that one timezone (Sao Paulo) is because of considerations specific to that timezone, namely daylight saving. Daylight saving in Brazil begins on the first Sunday in November, which happens to be November 1 in 2020.
The time that it would default to does not exist, so it returns nil.
You can play around with it by changing the formatter to include time and seeing exactly when the date formatter starts returning nil.
Have run into similar confusion before; Dates and time zones are tricky.
If you're using the date formatter for displaying dates to a user, you should typically not override their device settings and you probably want to avoid using a fixed date format, and should be aware of the potential for the date formatter to return a nil value (Swift optionals to the rescue). But if you're using it for internal dates, such as for your API, you should consider setting an explicit locale/time zone on your date formatter so these kinds of things don't happen, for example:
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
Hardware-dependent NSDateFormatter dateFromString: bug (returns nil)
It's because you are not setting the date formatter's locale to the special en_US_POSIX
locale. Most likely your iPhone 5 has a different setting for the 24-hour setting.
You need to set the special locale whenever you parse a fixed format string.
How do I format a date in JavaScript?
For custom-delimited date formats, you have to pull out the date (or time)
components from a DateTimeFormat
object (which is part of the
ECMAScript Internationalization API), and then manually create a string
with the delimiters you want.
To do this, you can use DateTimeFormat#formatToParts
. You could
destructure the array, but that is not ideal, as the array output depends on the
locale:
{ // example 1
let f = new Intl.DateTimeFormat('en');
let a = f.formatToParts();
console.log(a);
}
{ // example 2
let f = new Intl.DateTimeFormat('hi');
let a = f.formatToParts();
console.log(a);
}
Related Topics
How to Keep Uiswitch State When Changing Viewcontrollers
Alternative to Usernotificationcenterdelegate's Willpresent When App Is in Background
Could Not Insert New Outlet Connection: Could Not Find Any Information for the Class Named
Delete Specified File from Document Directory
Could Not Launch Process Launch Failed: Timed Out Waiting for App to Launch
How to Listen for All Notifications Sent to the iOS Nsnotificationcenter's Defaultcenter
Local and Push Notifications in iOS Version Compatible
How to Set Initial Values for Nsuserdefault Keys
The Default App Has Not Been Configured Yet
Where Do I Find iOS Obj-C Code to Scan and Connect to Wifi (Private API)
How to Convert an Int to a Character in Swift
Add a Watermark on Video After Merging Video and Audio Asset into One in Swift3 iOS
iOS 11 Safe Area Layout Guide Backwards Compatibility
Warning Frame for "Navigation Bar" Will Be Different at Run Time Appears in Xcode 8 Swift 3
Alamofire Download in Background Session