56

Am learning swift and am struck in converting the date String to NSDate to string. Am getting the date string in this format "Thu, 22 Oct 2015 07:45:17 +0000". I need to show the date in the MM-dd-yyyy format. I tried the following code but, it returns "null".

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
let dateObj = dateFormatter.dateFromString(dateString!)
print("Dateobj: \(dateObj)")

Can anyone please help where am going wrong? Looking forward the help. Thanks in advance.

Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100
  • 3
    read this document if any one want to know about the date formate http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns – Rizwan Shaikh Oct 22 '15 at 10:07

4 Answers4

115

First, you need to convert your string to NSDate with its format. Then, you change the dateFormatter to your simple format and convert it back to a String.

Swift 3

let dateString = "Thu, 22 Oct 2015 07:45:17 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "en_GB")

let dateObj = dateFormatter.date(from: dateString)

dateFormatter.dateFormat = "MM-dd-yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")

The printed result is: Dateobj: 10-22-2015

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
t4nhpt
  • 5,264
  • 4
  • 34
  • 43
  • 3
    In `Swift 3` is quite similar, instead of `dateFormatter.stringFromDate(date)` is `dateFormatter.string(from:date)`. – kikettas Oct 24 '16 at 22:33
  • 1
    This will crash if your device is not in english because of "Thu", you have to specify the locale. In swift 3 : `dateFormatter.locale = Locale.init(identifier: "en_GB")` – Aximem Jan 16 '17 at 08:57
  • NSDateFormatter has been replaced with DateFormatter – mbdavis Jan 16 '17 at 13:15
  • 1
    NOTE: This answer is incorrect if the date should be localized. For example in various countries month and day are swapped – Gargo Nov 13 '17 at 19:36
  • This is using the wrong locale. You need to use `en_US_POSIX` when using the date formatter to convert a fixed format string into a `Date`. – rmaddy Jun 23 '18 at 23:38
  • @Gargo This is a great example of why you should never use `dateFormat` to convert a `Date` into a string presented to a user. Always use `dateStyle` and `timeStyle`. – rmaddy Jun 23 '18 at 23:50
51
//String to Date Convert

var dateString = "2014-01-12"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.dateFromString(dateString)
println(s)


//CONVERT FROM NSDate to String  

let date = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd" 
var dateString = dateFormatter.stringFromDate(date)
println(dateString)  
rmaddy
  • 314,917
  • 42
  • 532
  • 579
8

Swift 2 and below

let date = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
var dateString = dateFormatter.stringFromDate(date)
println(dateString)

And in Swift 3 and higher this would now be written as:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
var dateString = dateFormatter.string(from: date)
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
brockk
  • 116
  • 3
4

See answer from Gary Makin. And you need change the format or data. Because the data that you have do not fit under the chosen format. For example this code works correct:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let dateObj = dateFormatter.dateFromString("10 10 2001")
print("Dateobj: \(dateObj)")
Alexey Pichukov
  • 3,377
  • 2
  • 19
  • 22