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,131
  • 1
  • 8
  • 27

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
  • 510,973
  • 84
  • 1,183
  • 1,314
rmaddy
  • 307,833
  • 40
  • 508
  • 550