Created
March 25, 2021 02:58
-
-
Save imjhk03/65da93bce8a9d6614f62b8bff4d2360e to your computer and use it in GitHub Desktop.
Useful String and Date extensions
This file contains hidden or 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 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