Skip to content

Instantly share code, notes, and snippets.

View germanger's full-sized avatar

Germán Vicencio L. germanger

View GitHub Profile
@germanger
germanger / prettifyDate.swift
Last active August 29, 2015 14:21 — forked from minorbug/timeago.swift
Prettify swift date relative to now: "In 20 seconds", "A year ago", "Tomorrow", "Yesterday", etc.
func prettifyDate(date:NSDate, numericDates:Bool) -> String {
let calendar = NSCalendar.currentCalendar()
let unitFlags = NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitWeekOfYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitSecond
let now = NSDate()
let earliest = now.earlierDate(date)
let latest = (earliest == now) ? date : now
let components:NSDateComponents = calendar.components(unitFlags, fromDate: earliest, toDate: latest, options: nil)
let past = earliest == date
if (components.year >= 2) {