-1

In swift 2.3, I wrote a function that takes the date 6-01-16, converts it to NSDate, then utilizes that in a CKQuery. I'm not sure how to update this for Swift 3.

Here's my current code:

let formatter = DateFormatter()
    formatter.dateFormat = "yyyymmdd"
    let olddate = formatter.date(from: "20160601")
    print(olddate)

This isn't working, how should I change my syntax?

Alex
  • 141
  • 2
  • 10
  • 2
    How is it "not working"? Syntax error, wrong result? Note that your month format is wrong (it should be "MM") so this wouldn't work in Swift 2.3 either. – Martin R Sep 09 '16 at 14:34
  • Ah, found the issue somewhere else. I'm trying to access the field "sortingDate" in a CKRecord which I know exists with "var prevFact = birdFacts[0].value(forKey: "sortingDate") as! Int". Is this syntax correct? – Alex Sep 09 '16 at 15:11

1 Answers1

8

mm is the format for minutes; you mean MM for months. Your dateFormat should be:

"yyyyMMdd"
andyvn22
  • 14,696
  • 1
  • 52
  • 74