1

I'm trying to parse a String in the format "ddMMyy-HH:mm:ss" using the following code

static func parseDate(var date: String?) -> NSDate?{
        if(date == nil){
            return nil
        }
        print("PARSING \(date)")
        let formatter = NSDateFormatter()
        formatter.dateFormat = "ddMMyy-HH:mm:ss"
        let res = formatter.dateFromString(date!)
        print("RESULT \(res)")
        return res
    }

The value of res is nil at the end, even though date is not nil. I then tried removing the : and - symbols using

  date = date!.stringByReplacingOccurrencesOfString(":", withString: "")
  date = date!.stringByReplacingOccurrencesOfString("-", withString: "")

And then changing my format to reflect this

formatter.dateFormat = "ddMMyyHHmmss"

The console output now is

TIME IS 291015-12:10:12
PARSING Optional("291015121012")
RESULT nil

I also tried adding the line

formatter.locale = NSLocale.currentLocale()

The strange thing is, the app works perfectly on iOS 9 and I think it worked on 8 too.

Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46

1 Answers1

0

Turns out the issue was nothing to do with iOS, it was to do with the fact that my ios7 device happened to be in 12 hour mode in device settings. This confused the formatter. Using

    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

as suggested by @WiliamKinaan fixed this

Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46