-1

I've a specific time format which I want to use to generate a Date object. This is the format:

Fri, 17 Mar 2017 08:42:00 +0100

Here is my Swift code that should create the data object:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
let d = dateFormatter.date(from: value)

The generation of the Date object with dateFormatter.date does always return nil. But I cant see what's wrong in my formatting string. Can anyone see my error?

Fabian
  • 492
  • 6
  • 20

1 Answers1

1

I have test it and works perfectly.

Could you test this code line to identify you Locale configuration?

print(Locale.current.identifier)

I only made a little change related to the Optional result that DateFormatter method returns

import Foundation

let the_date: String = "Fri, 17 Mar 2017 08:42:00 +0100"

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"

if let d = dateFormatter.date(from: the_date)
{
    print(d)
}

enter image description here

Adolfo
  • 1,862
  • 13
  • 19
  • Stating that you cannot reproduce the problem described in the question is not an answer, compare https://meta.stackoverflow.com/a/277924/1187415. – Martin R Mar 17 '17 at 08:46
  • Hi @MartinR. Even if you suggest a code change? Should I delete my post? It's my first "described in the question is not an answer" issue :-) – Adolfo Mar 17 '17 at 08:49