Datefromstring Returns Nil for Some Values

dateFromString returns nil for some values

format should be HH for 24 hours even you are getting values only for 12 hours.

frmt1.dateFormat = "MMM, dd yyyy HH:mm a"

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.

NSDate dateFromString returns nil on Device Language Change

Going by the comments above, set the locale as follows

- (NSDate*) GetTime:(NSString*) txt
{
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
[df setDateFormat:@"hh:mm a"];

return [df dateFromString:txt];
}

This will ensure that no matter the devices locale, if you are passing it an english string for the time in the required format, it will properly create an NSDate.

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 and NSDateFormatter.timeStyle) or generate your date format string from a template (using NSDateFormatter.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.

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"

-[NSDateFormatter dateFromString:] returns nil

Try setting [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];



Related Topics



Leave a reply



Submit