Search code examples
iosswiftnsdate

Not able to get date in string format


I'm storing some dates in coredata in Date format. In another viewcontroller, I want to retrieve those dates and convert them to string. I tried to achieve it like so...

if let bday = result.birthday {

print(bday)
let formatter = DateFormatter()
let bDateString = formatter.string(from: bday as Date)
print(bDateString)
self.birthdate = bDateString

}

Here, printing bday gives the proper date. But printing bDateString after converting to string gives nil. What am I doing wrong...?


Solution

  • Please provide format of date like this :

    formatter.dateFormat = "MM-dd-yyyy" //provide your date format here