Skip to content

Instantly share code, notes, and snippets.

@tanner0101
Last active November 21, 2015 16:18
Show Gist options
  • Save tanner0101/e5d2e0bab5a58b938b08 to your computer and use it in GitHub Desktop.
Save tanner0101/e5d2e0bab5a58b938b08 to your computer and use it in GitHub Desktop.
Return human readable string for any NSTimeInterval
/**
NSTimeIntervalExtensions.swift
Return human readable string for any NSTimeInterval
<https://gist.github.com/tannernelson/e5d2e0bab5a58b938b08>
*/
import Foundation
public extension NSTimeInterval {
var readable: String {
get {
var second = 1
var minute = second * 60
var hour = minute * 60
var day = hour * 24
var num: Int = Int(abs(self))
var unit = "day"
if num >= day {
num /= day
} else if num >= hour {
num /= hour
unit = "hour"
} else if num >= minute {
num /= minute
unit = "minute"
} else if num >= second {
num /= second
unit = "second"
} else {
num = 0
}
if num > 1 {
unit += "s"
}
if num == 0 {
return "now"
} else {
return "\(num) \(unit)"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment