1

I am having the user pick a date and time and saving that as a string I display in a text field, like so:

@objc func dealDateSelected(sender: UIDatePicker) {
    let date = DateFormatter()
    date.dateStyle = .short
    date.timeStyle = .short
    date.timeZone = TimeZone.current
    date.locale = Locale.current

    limitedTimeTextField.text = date.string(from: sender.date)
}

Then I am attempting to convert that text field back to a date object later, but I cannot get it to work. The format looks like this: "6/7/20, 10:10 PM". All of the things I've seen to convert date are in a different format than what I'm using. I saw something about kCFDateFormatterShortStyle but I cannot find where to access it.

When I try this

//Check Time
var dateEnding: Date?
if(limitedTimeSwitch.isOn) {
    if let dateChosen = limitedTimeTextField.text {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone.current
        formatter.locale = Locale.current
        formatter.dateStyle = .short
        formatter.timeStyle = .short

        dateEnding = formatter.date(from: dateChosen)
    }
}

I only get nil back. I've also tried using

dateEnding = ISO8601DateFormatter().date(from: dateChosen)

but that also returns nil. My string is coming through properly as "6/7/20, 10:10 PM", in that format.

What format do I need to use to get it converted back into a date? I thought it would be simple since all I'm doing is converting date to string, then back again, but it's causing so much trouble.

Showing off the nil value of conversion

ATCraiger
  • 109
  • 2
  • 13
  • 1
    I'm not able to reproduce the problem – I can successfully convert a date to a `String` and then back to a `Date` with the same date formatters as you. Are you sure the line `dateEnding = formatter.date(from: dateChosen)` is executing? Maybe it's not going into one of those two `if` statements. – TylerP Jun 07 '20 at 19:43
  • 1
    @Tyler I'm sure. I added a photo showing the nil value, and the code going through the if statement. I thought it should work this way, too. But it just fails for me.. – ATCraiger Jun 07 '20 at 19:47
  • @ATCraiger what happens if you use a hardcoded format `date.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"` to convert to string and back to date, does it work? – Claudio Jun 07 '20 at 20:06
  • How bizarre. This seems to be a problem with the debugger or something, as I can get it to show `dateEnding` as being nil, but the code execution proves that it's _not_ actually nil. See [this screenshot](https://i.imgur.com/Z07DV7e.png) that shows it going into the `else` block when checking if `dateEnding` is nil. – TylerP Jun 07 '20 at 20:17
  • @Claudio with the hard coded format, it still come out as nil. – ATCraiger Jun 07 '20 at 20:22
  • @ATCraiger I suggest you try adding the `if-else` statement like I did in my above comment to see if `dateEnding` is actually nil or if the debugger is lying to you (like it did to me). – TylerP Jun 07 '20 at 20:29
  • 1
    @Tyler I did, and it went to the not nil section. What in the world. I kept going through the debugger into a function where I passed that date and it was nil all the way through. I guess I'll keep going, and try to pass it onto my web server and see if it is nil there. – ATCraiger Jun 07 '20 at 20:33
  • 1
    Yep, looks like this might already be a known issue with Xcode 11: https://stackoverflow.com/questions/58108824/instantiated-optional-variable-shows-as-nil-in-xcode-debugger – TylerP Jun 07 '20 at 20:40
  • 1
    @Tyler Thanks for pointing that out. Now if only I could get back the last few hours of my life.. – ATCraiger Jun 07 '20 at 20:44

0 Answers0