1

I have a date like so:

2020-10-01T00:00:00

And I'm trying to format it using this:

let formatter = DateFormatter()

formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

let dateRequired = formatter.date(from: self.objects[indexPath.row]["DateRequired"] as! String)

But it always return nil.

Here is my full code, what am I doing wrong?

let formatter = DateFormatter()

formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

let dateRequired = formatter.date(from: self.objects[indexPath.row]["DateRequired"] as! String)
                
formatter.dateFormat = "yyyy-MM-dd"
                
let dateRequiredString = formatter.string(from: dateRequired!)

self.dateRequiredTextField.text = dateRequiredString
                
self.dateRequiredTextField.placeholder = "Date Required"
                
self.datePicker.datePickerMode = .date
                
let toolbar = UIToolbar()
                
toolbar.sizeToFit()
                
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.doneButtonPressed))
                
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
                
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(self.cancelButtonPressed))
                
toolbar.setItems([cancelButton, space, doneButton], animated: false)
                
self.dateRequiredTextField.inputAccessoryView = toolbar
                
self.dateRequiredTextField.inputView = self.datePicker
user979331
  • 11,039
  • 73
  • 223
  • 418

1 Answers1

1

There's nothing with your code as far as you've shown. For example, this works fine:

let s = "2020-10-01T00:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let dateRequired = formatter.date(from: s)

Since that works, we may conclude that self.objects[indexPath.row]["DateRequired"] is probably not what you think it is. Another possibility is that you need to set the locale:

let s = "2020-10-01T00:00:00"
let formatter = DateFormatter()
formatter.locale = Locale(identifier:"en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let dateRequired = formatter.date(from: s)

Note, however, that you should not be using DateFormatter here; this use case is exactly what ISO8601DateFormatter is for, and you'll be happier using it rather than trying to reinvent this wheel.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You should set the locale **before** setting the dateFormat – Leo Dabus Oct 16 '20 at 19:27
  • @LeoDabus Why? Nothing happens until you ask for the `date`, surely... But if true, you should just edit to fix! – matt Oct 16 '20 at 20:10
  • if you're working with fixed-format dates, **you should first set the locale of the date formatter** to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences. https://developer.apple.com/library/archive/qa/qa1480/_index.html – Leo Dabus Oct 16 '20 at 21:21