为什么日期总是在今天虽然我通过了不同的约会

时间:2016-01-18 21:51:08

标签: ios swift

这是我的代码

 let dateValue = formatterDate.dateFromString(request.date)
        print("da = \(request.date)")
        let dayValue = NSDate()
        let compnentsDate = myCalendar.components(.Year, fromDate: dateValue!)
        let componentsDay = myCalendar.components(.Year, fromDate: dayValue)
        if compnentsDate.year == componentsDay.year && compnentsDate.month == componentsDay.month && compnentsDate.day  == componentsDay.day {
            cell.dateLabel.text = "today"
        }else {
            if compnentsDate.year == componentsDay.year && compnentsDate.month == componentsDay.month && compnentsDate.day  == (componentsDay.day + 1 ){
            cell.dateLabel.text = "tomorrow"
            }else {
                cell.dateLabel.text = request.date
            }

        }

如你所见,我正在制作印刷品,而印刷品的价值是:

da = Jan, 25, 2016

但是,代码总是转到第一个if语句,并告诉我“今天”

为什么请?

我的代码有什么问题?

1 个答案:

答案 0 :(得分:5)

您需要在调用components:fromDate:函数时指定所需的所有组件。

您需要传递.Year.Month.Day,而不仅仅是.Year。像这样:

let compnentsDate = myCalendar.components([.Year , .Month , .Day], fromDate: dateValue!)
let componentsDay = myCalendar.components([.Year , .Month , .Day], fromDate: dayValue)