Skip to content

Instantly share code, notes, and snippets.

@raisiqueira
Created December 5, 2017 18:42
Show Gist options
  • Save raisiqueira/6032468fccd27209505474767612b3c8 to your computer and use it in GitHub Desktop.
Save raisiqueira/6032468fccd27209505474767612b3c8 to your computer and use it in GitHub Desktop.
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