0

I'm using SwiftDate (from malcommac.github) to validate string dates, but I find that it accepts as valid the 31st of ANY month (Feb, Nov, whatever). The 32nd of any month fails on a SwiftDate.DateError.FailedToParse.

The following is taken from an example on the SwiftDate GitHub page. The printed result is 01 dic 1999, 23:30:00 CET.

let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italian)
// Parse a string which a custom format
let date1 = try! DateInRegion(string: "1999-11-31 23:30:00", format: .custom("yyyy-MM-dd HH:mm:ss"), fromRegion: regionRome)
print(date1)

Am I overlooking something? I assume this result isn't intended.

William Hu
  • 15,423
  • 11
  • 100
  • 121
JGCR
  • 1
  • 3
  • When parsed, does it return the 31 of that month, or the _last_ day of the month? What's the output of `date1`? In some libs, specifying the 31st automatically returns the last day of the month. – brandonscript Dec 05 '16 at 04:20
  • 2
    This question should be asked of the developer through the Issues section of the project website. – rmaddy Dec 05 '16 at 04:21
  • It returns the 31st. The output of date1 is Dec 1. – JGCR Dec 05 '16 at 04:32
  • The GitHub SwiftDate page suggests "If you need help, use Stack Overflow. (Tag ‘swiftdate’)". – JGCR Dec 05 '16 at 04:33
  • @JGCR try `let dateStr = "1999-11-31 23:30:00" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" dateFormatter.calendar = Calendar(identifier: .gregorian) dateFormatter.date(from: dateStr) // "Dec 1, 1999, 11:30 PM"` same result – Leo Dabus Dec 05 '16 at 04:51
  • @Leo Dabus So, does this mean that we can't use SwiftDate OR DateFormatter for date validation? – JGCR Dec 05 '16 at 05:17
  • 1
    @JGCR yes you can you just need to set isLenient to false `dateFormatter.isLenient = false` – Leo Dabus Dec 05 '16 at 05:20
  • Example for isLenient: http://stackoverflow.com/a/39730502/1187415. – Martin R Dec 05 '16 at 05:21
  • @Leo Dabus Yes, 'isLenient' solves the problem when using dateFormatter. I can't see that there is anything similar in SwiftDate. I'll keep sniffing around. – JGCR Dec 05 '16 at 06:25

0 Answers0