0

I'm facing an issues and I can't seem to find a solution for it. There is a date picker and two buttons to set a start date and an end date. My app calculates the difference between these two dates.

Unfortunately, the calculated difference is not correct. For example when I set 5 Nov, 2016 as my first date and 6 Nov, 2016 as my second date, the code returns a difference of 0 days. When I change the second date to 7 Nov, 2016 it returns a difference of 1 day etc. But when I do it the other way by setting first lastDate to 6 Nov, 2016 and then firstDate to 5 Nov, 2016 the difference is being calculated properly (the output is 1 day). It somehow depends on which date you set first.

Here is my code:

@IBOutlet weak var startDateOutput: UILabel!
@IBOutlet weak var endDateOutput: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var answerFieldTimeDifference: UILabel!

var firstDate:NSDate?
var lastDate:NSDate?

@IBAction func startButton(sender: AnyObject)
{
    firstDate = datePicker.date
    let dateStr = NSDateFormatter.localizedStringFromDate(firstDate!, dateStyle: NSDateFormatterStyle.MediumStyle, timeStyle: NSDateFormatterStyle.NoStyle)
    startDateOutput.text = dateStr

    daysBetweenDates()
}

@IBAction func expirationButton(sender: AnyObject)
{
    lastDate = datePicker.date
    let dateStr = NSDateFormatter.localizedStringFromDate(lastDate!, dateStyle: NSDateFormatterStyle.MediumStyle, timeStyle: NSDateFormatterStyle.NoStyle)
    endDateOutput.text = dateStr 

    daysBetweenDates()
}

func daysBetweenDates()
{
    if var firstDate = firstDate, var lastDate = lastDate
    {        
        let calendar = NSCalendar.currentCalendar()

        let firstDate = calendar.startOfDayForDate(firstDate)
        let secondDate = calendar.startOfDayForDate(lastdate)

        let components = calendar.components([.Day], fromDate: date1, toDate: date2, options: [])

        var differenceInDays = components.day

        answerFieldTimeDifference.text = String(differenceInDays)
    }    
}
  • 2
    Date formatting and string processing is not the proper way to do date calculations. Use the `NSCalendar` class to do proper date calculations. – rmaddy Nov 05 '16 at 20:55
  • See http://stackoverflow.com/questions/24723431/swift-days-between-two-nsdates for a proper solution. – rmaddy Nov 05 '16 at 20:56
  • I tried this new method, but when I want to set one date it crashes immediately. Can you help me? – heisenberg Nov 05 '16 at 22:51
  • No one can help if you don't provide relevant details. Update your question as needed. – rmaddy Nov 05 '16 at 22:51
  • I edited the question. – heisenberg Nov 05 '16 at 22:58
  • Use the debugger. Where does it crash? Check the values of the variables. – rmaddy Nov 06 '16 at 02:58
  • @rmaddy I'm not quite sure if this might be the problem: When I press the first button to set the date for `firstDate`, the `lastDate` is still nil. It probably tries to calculate the difference by calling the function `daysBetweenDates()` but there is no date set for `lastDate`. Can you help me? https://img5.picload.org/image/rdiiccaw/error2.png Here is a screenshot of my debugger. – heisenberg Nov 06 '16 at 12:05
  • Note the line `if var firstDate = firstDate, var lastDate = lastDate` that you used to have. You still need that line. – rmaddy Nov 06 '16 at 15:52
  • @rmaddy Okay, it works now. Thank you very much. Unfortunately, I still have the problem with incorrect calculation as I mentioned in my main post. When I press the first button to set `firstDate` (e.g. `6 Nov, 2016`) and then the second button to set `lastDate` (e.g. `7 Nov, 2016`) it still returns `0` days. But when I do it in reverse (firstly set `lastDate` to `7 Nov, 2016` and then set `firstDate` to `6 Nov, 2016`) the output becomes correct and it returns a difference of `1` day. Do you know how to fix this? Thank you. – heisenberg Nov 06 '16 at 18:30

1 Answers1

0

I found the solution to my problem.

I just added the following two lines into the daysBetweenDates() function

let firstDate = calendar.startOfDayForDate(firstDate)
let secondDate = calendar.startOfDayForDate(lastdate)

The code above is updated and should now calculate the difference correctly.