iosswiftswift3swift3.2

Get date from core data, then fetch and display in the tableViewCell


How to display data in the tableViewCell, which i'm fetching from core data as date? Here is my code, but it doesn't work

let formatter = DateFormatter()
// initially set the format based on your datepicker date
formatter.dateFormat = "yyyy/MM/dd  HH:mm:ss"
    
let myString = formatter.string(from: game.date! as Date)
// convert your string to date
print("str - \(myString)")
let yourDate = formatter.date(from: myString)
print("date - \(String(describing: yourDate!))")
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd/MMM/yyyy  HH:mm:ss"
// again convert your date to string
let myStringDate = formatter.string(from: yourDate!)
cell.detailTextLabel?.text = myStringDate

Date is fetching Here is an output

str - 2017/07/19 13:26:40

date - 2017-07-19 10:26:40 +0000

An error is

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)


Solution

  • I tried like this in the play ground and i worked

    let date = Date()
    let myString = formatter.string(from: date)
    // convert your string to date
    print("str - \(myString)")
    let yourDate = formatter.date(from: myString)
    print("date - \(String(describing: yourDate!))")
    //then again set the date format whhich type of output you need
    formatter.dateFormat = "dd/MMM/yyyy  HH:mm:ss"
    // again convert your date to string
    let myStringDate = formatter.string(from: yourDate!)
    print("my string = \(myString)")
    

    in the console it is printing "my string" value.