Last active
July 4, 2019 13:43
-
-
Save ppth0608/40197e5b5b356727e7365d2a5d594bd3 to your computer and use it in GitHub Desktop.
String to date, Date to String
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 Foundation | |
exntension Date { | |
func toString(format: String) -> String { | |
let formatter = DateFormatter() | |
formatter.calendar = Calendar(identifier: .gregorian) | |
formatter.dateFormat = format | |
return formatter.string(from: self) | |
} | |
} | |
extension String { | |
func toDate(format: String, locale: Locale? = Locale(identifier: "en_US")) -> Date? { | |
let dateFormatter = DateFormatter() | |
let calendar = Calendar(identifier: .gregorian) | |
dateFormatter.calendar = calendar | |
dateFormatter.dateFormat = format | |
dateFormatter.locale = locale | |
return dateFormatter.date(from: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment