Skip to content

Instantly share code, notes, and snippets.

@anirudhamahale
Last active February 27, 2020 11:21
Show Gist options
  • Save anirudhamahale/6e13bb15db60af16aec217e117055f5d to your computer and use it in GitHub Desktop.
Save anirudhamahale/6e13bb15db60af16aec217e117055f5d to your computer and use it in GitHub Desktop.
Compare Date
// Set date somewhere
UserDefaults.standard.set(Date(), forKey:"creationTime")
extension Date {
func isBeyond24Hours() -> Bool {
if let date = UserDefaults.standard.object(forKey: "creationTime") as? Date {
if let diff = Calendar.current.dateComponents([.hour], from: date, to: Date()).hour, diff > 24 {
return true
}
return false
}
return false
}
/// Converts the Date object to String from given format.
/// - Parameter format: Date Format.
func convertTo(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
/// Converts the Date object to UTC String from given format.
/// - Parameter format: Date Format.
func convertToUTC(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.timeZone = TimeZone(abbreviation: "UTC")
return formatter.string(from: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment