Skip to content

Instantly share code, notes, and snippets.

View indrajitsinh's full-sized avatar

CodeNinja indrajitsinh

View GitHub Profile
@indrajitsinh
indrajitsinh / timeago.swift
Last active June 17, 2016 14:36 — forked from jacks205/timeago.swift
"Time ago" function for Swift (based on MatthewYork's DateTools for Objective-C) *Swift 3
func timeAgoSinceDate(date:NSDate, numericDates:Bool) -> String {
let calendar = Calendar.current()
let now = NSDate()
let earliest = now.earlierDate(date as Date)
let latest = (earliest == now) ? date : now
let components:NSDateComponents = calendar.components([Calendar.Unit.minute , Calendar.Unit.hour , Calendar.Unit.day , Calendar.Unit.weekOfYear , Calendar.Unit.month , Calendar.Unit.year ,Calendar.Unit.second], from: earliest, to: latest as Date, options: Calendar.Options())
if (components.year >= 2) {
return "\(components.year) years ago"