Skip to content

Instantly share code, notes, and snippets.

@imjhk03
Created March 25, 2021 02:58
Show Gist options
  • Select an option

  • Save imjhk03/65da93bce8a9d6614f62b8bff4d2360e to your computer and use it in GitHub Desktop.

Select an option

Save imjhk03/65da93bce8a9d6614f62b8bff4d2360e to your computer and use it in GitHub Desktop.
Useful String and Date extensions
import Foundation
extension String {
func toDate(format: String) -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.date(from: self)
}
}
extension Date {
func toRelativeLocalizeString() -> String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
let relativeDate = formatter.localizedString(for: self, relativeTo: Date())
return relativeDate
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment