1

A problem has arisen with my program in which it crashes on some specific devices. I've managed to track the problem to a dateFormatter being nil when trying to convert a string to a date. This is only only some devices however.

My question is, how is this possible and is there a problem with how I format my date?

    func checkIfActive(start: String, end: String) -> Bool
{

    let dateFormatterPrint = DateFormatter()
    dateFormatterPrint.dateFormat = "MMMM dd,yyyy hh:mma"


    let dateStart = dateFormatterPrint.date(from: start)
    let dateEnd = dateFormatterPrint.date(from: end)

    print("start time string\(start)") // This print March 19,2018 08:33PM
    print("start date\(dateStart)") // This is nil some devices, however works fine on others



        if(Date().isBetween(date: dateStart!, andDate: dateEnd!))
        {
            return true
        }

    return false
}
madmanprogram
  • 51
  • 1
  • 7

1 Answers1

3

I've had this issue a couple of times, only on an iPhone 8.

To fix it you just need to give the DateFormatter a locale, e.g.:

dateFormatterPrint.locale = Locale(identifier: "en_US_POSIX")
Leon
  • 3,614
  • 1
  • 33
  • 46