Question: How do you create a date object from a date in swift xcode.

eg in javascript you would do:
 `var day = new Date('2014-05-20');`

Answer: 
In, Swift 3.0 you have set date object for this way.

```
extension Date
{
    init(dateString:String) {
        let dateStringFormatter = DateFormatter()
        dateStringFormatter.dateFormat = "yyyy-MM-dd"
        dateStringFormatter.locale = Locale(identifier: "en_US_POSIX")
        let d = dateStringFormatter.date(from: dateString)!
        self(timeInterval:0, since:d)
    }
}

```
Personally I think it should be a failable initialiser:

```
extension Date {

    init?(dateString: String) {
        let dateStringFormatter = DateFormatter()
        dateStringFormatter.dateFormat = "yyyy-MM-dd"
        if let d = dateStringFormatter.date(from: dateString) {
            self.init(timeInterval: 0, since: d)
        } else {
            return nil
        }
    }
}

```

Otherwise a string with an invalid format will raise an exception.