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: 
This is best done using an extension to the existing `NSDate` class.

The following extension adds a new initializer which will create a date in the current locale using the date string in the format you specified.

```
extension NSDate
{
    convenience
      init(dateString:String) {
      let dateStringFormatter = NSDateFormatter()
      dateStringFormatter.dateFormat = "yyyy-MM-dd"
      dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
      let d = dateStringFormatter.dateFromString(dateString)!
      self.init(timeInterval:0, sinceDate:d)
    }
 }

```

Now you can create an NSDate from Swift just by doing:

```
NSDate(dateString:"2014-06-06")

```

Please note that this implementation does not cache the NSDateFormatter, which you might want to do for performance reasons if you expect to be creating many `NSDate`s in this way.

Please also note that this implementation will simply crash if you try to initialize an `NSDate` by passing in a string that cannot be parsed correctly. This is because of the forced unwrap of the optional value returned by `dateFromString`. If you wanted to return a `nil` on bad parses, you would ideally use a failible initializer; but you cannot do that now (June 2015), because of a limitation in Swift 1.2, so then you're next best choice is to use a class factory method.

A more elaborate example, which addresses both issues, is here: <https://gist.github.com/algal/09b08515460b7bd229fa> .

---

Update for Swift 5
------------------

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

```
According to Apple's [Data Formatting Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW10)

> 
> Creating a date formatter is not a cheap operation. If you are likely
> to use a formatter frequently, it is typically more efficient to cache
> a single instance than to create and dispose of multiple instances.
> One approach is to use a static variable
> 
> 
> 

And while I agree with @Leon that this should preferably be a failable initializer, it doesn't always have to be (just like there is `UIImage(imageLiteralResourceName:)` that will crash if the resource doesn't exist).

So here's my approach:

```
extension DateFormatter {
  static let yyyyMMdd: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    formatter.calendar = Calendar(identifier: .iso8601)
    formatter.timeZone = TimeZone(secondsFromGMT: 0)
    formatter.locale = Locale(identifier: "en_US_POSIX")
    return formatter
  }()
}

extension Date {
    init?(yyyyMMdd: String) {
        guard let date = DateFormatter.yyyyMMdd.date(from: yyyyMMdd) else { return nil }
        self.init(timeInterval: 0, since: date)
    }

    init(dateLiteralString yyyyMMdd: String) {
        let date = DateFormatter.yyyyMMdd.date(from: yyyyMMdd)!
        self.init(timeInterval: 0, since: date)
    }
}

```

And now enjoy simply calling:

```
// For cases where the date exists for sure (eg. seed data)
Date(dateLiteralString: "2020-03-30")

// The rest of the time (eg. parsing unknown data)
guard let date = Date(yyyyMMdd: "2020-03-30") else { return nil }

```