Last active
October 27, 2015 05:21
-
-
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....
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
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