Esqarrouth / EZSwiftExtensions

:smirk: How Swift standard types and classes were supposed to work.
MIT License
3k stars 381 forks source link

Error on DateExtensions.swift #448

Closed noriega9807 closed 6 years ago

noriega9807 commented 7 years ago

Hi, I was using the Date extension like this:

let format = "yyyy-MM-dd HH:mm:ss"
let fecha1 = Date(fromString: "2017-10-03 14:30:01", format: format)

And it was running fine on the simulator, however when running on a actual device it always returned nil so I modified DateExtensions.swift like this to get this working on a device:

public init?(fromString string: String, format: String) {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone.current
        formatter.locale = Locale(identifier: "en_US")
        formatter.dateFormat = format
        if let date = formatter.date(from: string) {
            self = date
        } else {
            return nil
        }
    }
Khalian commented 6 years ago

I might be able to work this in. My only concern is en_US. I think i am gonna make that a default param (although feels kinda partial to US folks).

Khalian commented 6 years ago

Let me know if this works for you ? https://github.com/goktugyil/EZSwiftExtensions/pull/466.