0

I'm having an issue where an attempt to turn a string into a date via a DateFromatter has produced nothing but 'nil' and I don't know where I'm going wrong. The code is as simple as can be:

    let testDate = "2021"

    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.dateFormat = "yyyy"

    formatter.timeZone = TimeZone(secondsFromGMT: 0)


    let date = formatter.date(from: testDate)

Note that this is much simpler than originally, the date I'm trying to format is actually:

"2021-05-01T01:00:00Z"

But I've stripped it right down to narrow down where the issue is.

As you can see from above, I've stripped down to a year, configured the DateFormatter with en_US_POSIX and used only the 'yyyy' as a format. This works in Playgrounds, but it doesn't work in my Xcode simulator (Which is in the US locale) or my own physical iPhone (set to UK locale). That being said, I've no idea why the locale would matter because the string in question is just a year - it'm not even getting a wrong year, just nil.

I'm not sure where I'm going wrong.

James Dobson
  • 1,191
  • 2
  • 8
  • 14
  • Does not work, sadly. I also tried .gregorian to no effect. – James Dobson Feb 01 '21 at 14:24
  • 1
    Make sure you are not passing an optional string and doing string interpolation with it. Also print your resulting date. It might appear to be nil but it is not because of a bug. – Leo Dabus Feb 01 '21 at 14:33
  • 1
    Thank you Leo, I just printed it out and it appeared - I was confused by the debugger representing it as nil and assumed there was something bizarre happening. If you post an answer, I will iupvote it and mark it as the solution – James Dobson Feb 01 '21 at 14:41

2 Answers2

3

Xcode Debugger has a bug as you can see in this post that it will show optional dates as nil even when parsing the date succeeds. If you print the optional date you will see the resulting date.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
0

please use correct format like this for date string:

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

instead of :

formatter.dateFormat = "yyyy"

each date string with specific formate like below code:

let testDate = "2021"
    formatter.dateFormat = "yyyy"

or

let testDate = "2021-05-01T01:00:00Z"
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

for more detail please see this link: https://stackoverflow.com/a/52297497/5140621

hessam
  • 404
  • 3
  • 10
  • 1
    How is "yyyy-MM-dd'T'HH:mm:ssZ" the correct format when the input string is "2021"? – Joakim Danielson Feb 01 '21 at 14:01
  • you can create date from "2021" then create date string from new date again create from new dateString with any format: let date = formatter.date(from: testDate) formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" let newDateString = formatter.string(from: date!) let newDate = formatter.date(from: newDateString) – hessam Feb 01 '21 at 14:11
  • 1
    I think you have slightly misunderstood the question – Joakim Danielson Feb 01 '21 at 14:17
  • I think you misunderstand the question - while knowing the correct format for the full date string is helpful, I haven't gotten the formatter to correctly produce a date using only the year which is where I am struggling. – James Dobson Feb 01 '21 at 14:17