Esqarrouth/EZSwiftExtensions

Date from string - init(fromString:format:) not returning correct month.

kevinmann opened this issue · 3 comments

I just noticed this bug while trying out the date functions. If I do something like this:

        let format1 = "dd-mm-yyyy hh:mm:ss"
        let customDate1 = Date(fromString: "11-05-2016 05:45:12", format: format1)
        print(customDate1)
        
        let format2 = "mm-dd-yyyy hh:mm:ss"
        let customDate2 = Date(fromString: "05-11-2016 05:45:12", format: format2)
        print(customDate2)

The outputs I get are incorrect, and both produce a date with a month value of 1. In the code above:

customDate1 prints: Optional(2016-01-11 05:45:12 +0000)
customDate2 prints: Optional(2016-01-11 05:45:12 +0000)

Edit: I am changed dd-mm-yyyy to dd-MM-yyyy for the format and it works however the tests in the documentation give an incorrect result still. From - EZSwiftExtensionsTests/DateTests.swift

    let format = "dd-mm-yyyy hh:mm:ss"
    [...]
    func testMonthAsString() {
        let customDate = Date(fromString: "09-01-2015 05:45:12", format: self.format)
        XCTAssertEqual(customDate?.monthAsString, "January")
    }

It will pass the test but only because it is testing for January if 09-01-2015 was changed to 09-03-2015 the month will still come back as January and not the expected March. I think it is just an incorrect string format used in the example.

Cool, I will take a look at this tomorrow. Thanks for catching this bug and sorry for the inconvenience.

Here is the PR for this #385

Hey we fixed this. Resolving.