1

How can I make it so that the cell shows the time it was created and edited?

I could not find anything about this

minutes / hours / days / weeks, etc.

Example:

Code part:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.nameText?.text = myData[indexPath.row]
    //
    let date = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
    let stringDate = formatter.string(from: date)
    //
    cell.NoneLabel?.text = stringDate
    return cell
}
Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
B2Fq
  • 876
  • 1
  • 8
  • 23
  • Just to confirm you question: You need the time passed since last edited/ created, is that right? For instance: "3 Minutes ago"; "5 days ago"? – dvp.petrov Feb 06 '18 at 14:22
  • @dvp.petrov Yes – B2Fq Feb 06 '18 at 14:23
  • [`doesRelativeDateFormatting `](https://developer.apple.com/documentation/foundation/dateformatter/1415848-doesrelativedateformatting)... meh. – holex Feb 06 '18 at 14:28

1 Answers1

4

You can get the Date() in the cellForRowAt method.

This Date() will give you the current date time when the cell was created.

let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy HH:mm"
let stringDate = formatter.string(from: date)

You can change the format with whatever you want.

Then with that you can calculate how much time ago the cell was created. To calculate that please check this link.

Matias Jurfest
  • 1,378
  • 16
  • 25