0

this is my code

 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
            }

        }

as you see I am making print, and the value of the print is:

da = Jan, 25, 2016

However, the code always goes to the first if statement, and tell me "today"

why please?

what is wrong in my code?

sarah
  • 1,201
  • 1
  • 9
  • 29

1 Answers1

5

You need to specify all of the components you need when you call the components:fromDate: function.

You need to pass .Year, .Month, and .Day, not just .Year. Something like this:

let compnentsDate = myCalendar.components([.Year , .Month , .Day], fromDate: dateValue!)
let componentsDay = myCalendar.components([.Year , .Month , .Day], fromDate: dayValue)
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
rmaddy
  • 314,917
  • 42
  • 532
  • 579