-
-
Save germanger/44a3e7aa1de465b264af to your computer and use it in GitHub Desktop.
Prettify swift date relative to now: "In 20 seconds", "A year ago", "Tomorrow", "Yesterday", etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
return past ? "\(components.year) years ago" : "In \(components.year) years" | |
} else if (components.year >= 1){ | |
if (numericDates){ | |
return past ? "1 year ago" : "In 1 year" | |
} else { | |
return past ? "Last year" : "In a year" | |
} | |
} else if (components.month >= 2) { | |
return past ? "\(components.month) months ago" : "In \(components.month) months" | |
} else if (components.month >= 1){ | |
if (numericDates){ | |
return past ? "1 month ago" : "In 1 month" | |
} else { | |
return past ? "Last month" : "In a month" | |
} | |
} else if (components.weekOfYear >= 2) { | |
return past ? "\(components.weekOfYear) weeks ago" : "In \(components.weekOfYear) weeks" | |
} else if (components.weekOfYear >= 1){ | |
if (numericDates){ | |
return past ? "1 week ago" : "In 1 week" | |
} else { | |
return past ? "Last week" : "In a week" | |
} | |
} else if (components.day >= 2) { | |
return past ? "\(components.day) days ago" : "In \(components.day) days" | |
} else if (components.day >= 1){ | |
if (numericDates){ | |
return past ? "1 day ago" : "In 1 day" | |
} else { | |
return past ? "Yesterday" : "Tomorrow" | |
} | |
} else if (components.hour >= 2) { | |
return past ? "\(components.hour) hours ago" : "In \(components.hour) hours" | |
} else if (components.hour >= 1){ | |
if (numericDates){ | |
return past ? "1 hour ago" : "In 1 hour" | |
} else { | |
return past ? "An hour ago" : "In an hour" | |
} | |
} else if (components.minute >= 2) { | |
return "\(components.minute) minutes ago" | |
} else if (components.minute >= 1){ | |
if (numericDates){ | |
return past ? "1 minute ago" : "In 1 minute" | |
} else { | |
return past ? "A minute ago" : "In a minute" | |
} | |
} else if (components.second >= 3) { | |
return past ? "\(components.second) seconds ago" : "In \(components.second) seconds" | |
} else { | |
return "Just now" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment