Skip to content

Instantly share code, notes, and snippets.

@lapfelix
Last active October 27, 2015 05:21
Show Gist options
  • Save lapfelix/2a694067f5168d2d5cc2 to your computer and use it in GitHub Desktop.
Save lapfelix/2a694067f5168d2d5cc2 to your computer and use it in GitHub Desktop.
A light NSDateFormatter subclass that's easy to use and understand. Though it probably won't work well with localization....
import UIKit
class FLPRelativeDateFormatter: NSDateFormatter {
static let sharedFormatter = FLPRelativeDateFormatter()
override func stringFromDate(date: NSDate) -> String {
let interval = date.timeIntervalSinceNow
if (interval < 61){
return "Just now"
}else if(interval < 3600){
let amount = Int((interval/60))
return ("\(amount) minute"+((amount>1) ? "s" : "")+" ago")
}else if(interval < 86400){
let amount = Int((interval/3600))
return ("\(amount) hour"+((amount>1) ? "s" : "")+" ago")
}else if(interval < 604800){
let amount = Int((interval/86400))
return ("\(amount) day"+((amount>1) ? "s" : "")+" ago")
}else {
return super.stringFromDate(date)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment