-7

I want to convert December 20, 2016 to 12/20/16.

December 20 is in String format. Not too sure if I can use NSDateFormatter for this. I do not what MM/DD/YYYY to something like YYYY:DD:MM. I want to convert a String December 20, 2016, and get it to 12/20/16.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Andy.W
  • 11
  • 3
  • Possible duplicate of http://stackoverflow.com/questions/37923481/convert-date-string-swift?noredirect=1&lq=1 and http://stackoverflow.com/questions/31040718/convert-a-date-string-to-yyyy-mm-dd-hhmmss-format-in-android-swift and http://stackoverflow.com/questions/34826282/swift-convert-a-string-to-a-date-and-then-to-a-string-in-a-different-format and http://stackoverflow.com/questions/39159324/convert-date-formatter-to-another-date-formatter – rmaddy May 03 '17 at 02:26
  • When reviewing the answers in the duplicate, keep in mind you will need to adjust the formats to match your specific needs. – rmaddy May 03 '17 at 02:26
  • Based on the many examples of converting a date string in one format to another date string in a different format (which is what you are asking to do), [edit] your question with your attempted code and clearly explain what issues you are having. – rmaddy May 10 '17 at 23:06

1 Answers1

0

Yes you can use DateFormatter (without NS in Swift 3, the same applies for Date not NSDate). To properly parse your date string you need to use the date format "MMMM dd, yyyy" and set the date formatter locale to "en_US_POSIX"

let string = "December 20, 2016"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MMMM dd, yyyy"

if let date = formatter.date(from: dateString) {
    print(date)  // "2016-12-20 02:00:00 +0000\n"
    formatter.dateStyle = .short
    let stringFromDate = formatter.string(from: date)   // "12/20/16"
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • The question asks how to convert the first string into the second string. This is only half of the answer. And there must be many duplicates for this question. – rmaddy May 03 '17 at 00:52
  • The question is how to convert the date string (without time) to date. – Leo Dabus May 03 '17 at 02:03
  • That's what the title says but then the question shows two different date strings. – rmaddy May 03 '17 at 02:06
  • **String date to NSDate?** And the date format is totally different. I am pretty sure his goal is to get a date not a string – Leo Dabus May 03 '17 at 02:06
  • 1
    Exactly, two different date string formats. The question implies a conversion from one string to another string which is different from the question's title. Regardless, do we really need yet another date conversion question? Please vote to close such dupes instead of posting yet another answer that has been posted many, many, many times before. :) – rmaddy May 03 '17 at 02:09
  • I can try to find a better duplicate with a similar format – Leo Dabus May 03 '17 at 02:10
  • Why? Teach him to fish instead of giving him a fish. :) – rmaddy May 03 '17 at 02:11
  • I would have closed the question if I could find a question with the same date format but I still can't find it – Leo Dabus May 03 '17 at 02:15
  • @rmaddy, I have looked at all those posts prior to sending this question out. I want specifically a conversion from `December` to 12 rather than 12/23/16 to a different format in NSDate. I know the question is similar to others. But what I am looking for is different. – Andy.W May 10 '17 at 23:02