Convert Date to time ago in swift – Tutorial

Convert Date to time ago  in swift3 - Tutorial

Introduction to tutorial on Convert Date to time ago in swift 3

I in this tutorial I am going to share a short written extension of Date class that will convert given date and time with respect to current date time into seconds ago, minutes ago, days ago, and so on. If we are creating a feed based app or app showing listing of posts, then we have requirement to show time elapsed from the time of feed/post posting to the current time of the device.

Extension for Date that convert given date to time ago

Below is the code for the extension


How to use the Date extension in your code

Below are the steps that shows how to use the date extension in your original source code Step 1: Open your ViewController.swift and add extension code at the bottom of the file. Code is shown below

Step 2: In  ViewController.swift file, viewDidLoad function, we will use it as shown below. We are passing -180 to create date 3 minutes behind current date.

Read more articles

How to create UITableView with sections

Add Pull to refresh to UITableView using custom UIRefreshControl in swift