3

I can't seem to find any info on this flag, on StackOverflow or elsewhere on the web. Apple's own documentation only says:

If a formatter is set to be lenient, when parsing a string it uses heuristics to guess at the date which is intended. As with any guessing, it may get the result date wrong (that is, a date other than that which was intended).

Maybe I'm misunderstanding how this is supposed to work, but I can't get it to work at all. My guess was something like this (with a relatively easy date to parse):

> import Foundation
> let df = DateFormatter()
> df.isLenient = true
> df.date(from: "12:00 1/1/2001")
$R0: Date? = nil

No matter what I try, I get nil.

I also see there's a doesRelativeDateFormatting flag, which claims to support phrases such as “today” and “tomorrow”", but that doesn't seem to do anything, either:

> df.doesRelativeDateFormatting = true
> df.date(from: "today")
$R1: Date? = nil

Any ideas?

Nirav D
  • 71,513
  • 12
  • 161
  • 183
J. Cocoe
  • 881
  • 1
  • 10
  • 22
  • 1
    What you are probably looking for is NSDataDetector http://stackoverflow.com/questions/32595651/convert-from-date-string-in-unknown-format/32595941#32595941 – Leo Dabus Sep 27 '16 at 18:10
  • Leo: Good call. I'm looking to replace `allowsNaturalLanguage`, and `lenient` is next to it in the "Natural Language Support" section. – J. Cocoe Sep 27 '16 at 20:02

1 Answers1

6

Here is an example where the lenient option makes a difference:

let df = DateFormatter()
df.timeZone = TimeZone(secondsFromGMT: 0)
df.isLenient = true
df.dateFormat = "yyyy-MM-dd"
print(df.date(from: "2015-02-29")) // Optional(2015-03-01 00:00:00 +0000)

2015 is not a leap year, so there is no February 29. With isLenient = true, the date is interpreted as March 1. With isLenient = false it is rejected:

let df = DateFormatter()
df.timeZone = TimeZone(secondsFromGMT: 0)
df.isLenient = false
df.dateFormat = "yyyy-MM-dd"
print(df.date(from: "2015-02-29")) // nil
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Interesting. I'm a little confused, though: what does this have to do with "Natural Language Support"? Or is that just a documentation blooper? – J. Cocoe Sep 27 '16 at 20:03
  • @J.Cocoe: I don't know. The examples show how isLenient can be used, but do not answer the question about an allowsNaturalLanguage replacement. – Martin R Sep 27 '16 at 20:25
  • @J.Cocoe: If your *actual problem* is how to replace allowsNaturalLanguage then you should update the question (and title) accordingly (and not hide that information in the comments). At present you are just asking about "isLenient", so that could by an XY-problem. – Martin R Sep 28 '16 at 05:44
  • My *actual problem* is about 17 yaks above this, and completely inappropriate for a StackOverflow question. I thought that "can somebody describe this mostly-undocumented feature (which looks like it should do exactly what I want but I can't get it to work)" was a good question. Still do, in fact. Regardless of my "actual problem", I'm very curious to learn what this flag actually does. Is it just for handling any day number up to 49 by pushing it into the next month? – J. Cocoe Sep 28 '16 at 22:01
  • @J.Cocoe Any luck in figuring this out? I'm trying to understand the same variable on the NumberFormatter class. – Brandon Bradley Jul 18 '17 at 15:45