转换后日期比较为零

时间:2017-09-14 13:30:18

标签: ios swift date

我有以下代码:

 for dates in calendar.selectedDates {
        for dateOfEvent in myEventDate {
            formatter.dateFormat = "dd MM yyyy"
            let dateToCompare = formatter.string(from: dates)

            if dateOfEvent == dateToCompare {
                formatter.dateFormat = "dd MM yyyy"
                let comparingDate = formatter.date(from: dateToCompare)
}

有没有人可以向我解释为什么比较日期是零? 我失去了它......

UPDATE -------- 在这个for循环中一切正常。我从日历(日期)中获取用户选择的日期,而dateOfEvent是从coredata获取的日期(字符串)。直到if语句一切正常。 当我尝试以这种格式格式化日期" dd MM yyyy"结果是nil

enter image description here

-------- UPDATE

or dates in calendar.selectedDates {
        for dateOfEvent in myEventDate {
            formatter.dateFormat = "dd MM yyyy"
            let dateToCompare = formatter.string(from: dates)

            if dateOfEvent == dateToCompare {

                formatter.timeZone = Calendar.current.timeZone
                formatter.locale = Calendar.current.locale
                formatter.dateFormat = "dd MM yyyy"
                let comparingDate = formatter.date(from: dateToCompare)
                print(comparingDate)

0 个答案:

没有答案