Skip to content

Instantly share code, notes, and snippets.

@alfian0
Forked from hpstuff/DateHelper
Created December 11, 2016 09:15
Show Gist options
  • Save alfian0/6667682cb776ca4d4d9b0318597641d2 to your computer and use it in GitHub Desktop.
Save alfian0/6667682cb776ca4d4d9b0318597641d2 to your computer and use it in GitHub Desktop.
Swift
extension String {
func toDate(format: String) -> NSDate? {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.dateFromString(self)
}
}
extension NSDate {
func toString(format: String) -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.stringFromDate(self)
}
}
var a = "17:23"
var d = a.toDate("HH:mm")
d.toString("yyyy-MM-dd HH:mm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment