Created
December 5, 2017 18:42
-
-
Save raisiqueira/6032468fccd27209505474767612b3c8 to your computer and use it in GitHub Desktop.
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 Date { | |
enum DateFormat: String { | |
case short = "yyyy-MM-dd" | |
case long = "yyyy-MM-dd HH:mm:ss" | |
case time = "HH:mm:ss" | |
} | |
func toString(withFormat format: DateFormat) -> String{ | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = format.rawValue | |
return dateFormatter.string(from: self) | |
} | |
} | |
// MARK: Tests | |
let now = Date() | |
now.toString(withFormat: .long) // "2017-11-15 09:17:35" | |
now.toString(withFormat: .short) // "2017-11-15" | |
now.toString(withFormat: .time) // "09:18:00" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment