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

extension Date {
func getElapsedInterval() -> String {
let interval = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self, to: Date())
if let year = interval.year, year > 0 {
return year == 1 ? "\(year)" + " " + "year" :
"\(year)" + " " + "years"
} else if let month = interval.month, month > 0 {
return month == 1 ? "\(month)" + " " + "month" :
"\(month)" + " " + "months"
} else if let day = interval.day, day > 0 {
return day == 1 ? "\(day)" + " " + "day" :
"\(day)" + " " + "days"
} else if let hour = interval.hour, hour > 0 {
return hour == 1 ? "\(hour)" + " " + "hour" :
"\(hour)" + " " + "hours"
} else if let minute = interval.minute, minute > 0 {
return minute == 1 ? "\(minute)" + " " + "minute" :
"\(minute)" + " " + "minutes"
} else if let second = interval.second, second > 0 {
return second == 1 ? "\(second)" + " " + "second" :
"\(second)" + " " + "seconds"
} else {
return "a moment ago"
}
}
}

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
//
// ViewController.swift
// Date time ago
//
// Created by Aman Aggarwal on 3/24/17.
// Copyright © 2017 iostutorialjunction.com. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension Date {
func getElapsedInterval() -> String {
let interval = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self, to: Date())
if let year = interval.year, year > 0 {
return year == 1 ? "\(year)" + " " + "year" :
"\(year)" + " " + "years"
} else if let month = interval.month, month > 0 {
return month == 1 ? "\(month)" + " " + "month" :
"\(month)" + " " + "months"
} else if let day = interval.day, day > 0 {
return day == 1 ? "\(day)" + " " + "day" :
"\(day)" + " " + "days"
} else if let hour = interval.hour, hour > 0 {
return hour == 1 ? "\(hour)" + " " + "hour" :
"\(hour)" + " " + "hours"
} else if let minute = interval.minute, minute > 0 {
return minute == 1 ? "\(minute)" + " " + "minute" :
"\(minute)" + " " + "minutes"
} else if let second = interval.second, second > 0 {
return second == 1 ? "\(second)" + " " + "second" :
"\(second)" + " " + "seconds"
} else {
return "a moment ago"
}
}
}

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.

//
// ViewController.swift
// Date time ago
//
// Created by Aman Aggarwal on 3/24/17.
// Copyright © 2017 iostutorialjunction.com All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let date = Date(timeIntervalSinceNow: -180)
print("Time ago :", date.getElapsedInterval())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Read more articles

How to create UITableView with sections

Add Pull to refresh to UITableView using custom UIRefreshControl in swift