DateFormatter's returns nil for specific date strings without time in Swift
You just need to set your date formatter calendar property:
Xcode 8.2.1 • Swift 3.0.2
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "es")
dateFormatter.dateFormat = "dd 'de' MMMM"
dateFormatter.date(from: "8 de octubre") // "Oct 8, 2000, 1:00 AM"
Why does NSDateFormatter not return nil when given empty string ()
If dateFromString: can not parse the string, returns nil
The simple answer is, the dateFromString
does manage to parse an empty string
. If you try and put wrong data in the string
, say "0"
then you will get nil
.
Why is it happening? I assume that it is just they way the class is written, since the date formatter does skip missing data and try to complete it. There is probably not a case for "all is missing".
DateFormatter date from string returns nil
If you’re showing your date to the user, don’t set fixed date format string. If you are using fixed-format dates, fix your locale first.
For more details, see Technical Q&A QA1480 titled “NSDateFormatter and Internet Dates”. Below are relevant excerpts (formatting mine):
Q: I'm using NSDateFormatter to parse an Internet-style date, but this fails for some users in some regions. I've set a specific date format string; shouldn't that force NSDateFormatter to work independently of the user's region settings?
A: No. While setting a date format string will appear to work for most users, it's not the right solution to this problem. There are many places where format strings behave in unexpected ways. (…)
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.dateStyle
andNSDateFormatter.timeStyle
) or generate your date format string from a template (usingNSDateFormatter.setLocalizedDateFormatFromTemplate(String)
).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.
Xcode 11, swift. Dateformatter always return nil
Update:
It would appear that it is a bug in Xcode. Trying your code in the viewDidLoad
and setting a breakpoint causes the lldb
description of the date to be nil
, however it correctly prints the expected value out.
You can see more about the bug at this SO post, thanks to OOPer for the link.
Currently this bug is still occurring in Xcode 11.2-beta
A couple of points
Use .dateFormat
instead of .format
Use the correct quotation marks "
instead of ”
, also you should remove the space from your date format string.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2019-05-03")
NSDateFormatter dateFromString Always Returns nil
There are two issues here:
The format of date string the formatter is expecting (
@"yyyy-MM-dd hh:mm:ss"
) is different from the format of the date string you're trying to parse (@"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz"
).Setting the formatter's locale to
[NSLocale systemLocale]
is causing[dateFormat dateFromString:]
to returnnil
. Set it to[NSLocate currentLocale]
.
The full code for the formatter should be:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:@"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:dateString];
Related Topics
Detect If iOS App Is Downloaded from Apple's Testflight
How to Create an Nsdecimal Without Using Nsnumber and Creating Autoreleased Objects
Firestore Paginating Data + Snapshot Listener
Creating an Image Format with an Unknown Type Is an Error Objective-C Xcode 8
How to Access an Iboutlet from Another Class
Errors Building Xcode Project After Adding in Run Script Fatal Error: Lipo: Input File
Instagram API Prevents Redirection to iOS App Upon Successful Login with 400 Bad Request Error
Nearby Bluetooth Devices Using Swift 3.0
Why Does Unexpected Non-Void Return Value in Void Function Happen
Facebook App Invites Notification Not Working in iOS
Get the Callers Phone Number from an Incoming Call on Iphone
How to Convert Cmsamplebuffer to Data in Swift
Trigger a Method in Uiviewcontroller from Its View
How to Convert Video (In Gallery) to Nsdata? in Swift
Date Formats from Device Based on Locale