1

I have the code below to calculate the days difference between 2 dates. First view is where i pick a date from UIDatePicker() , press a button to segue to the second view and to calculate days difference between the selected date and today's date and pass the result to the second view to be shown.

I just cant get the function to calculate the time difference to work. Is there something wrong im doing? Im sorry in advance as i've just started learning swift.

on my code

Calendar.current.dateComponents([.day], from: self.GetDate,to: datePicker.date).day! 

i get this error below

Type 'Any' has no member 'day'

-

first view.

import UIKit class ViewController: UIViewController { @IBOutlet weak var dateInput: UITextField! let datePicker = UIDatePicker() var GetDate = "" override func viewDidLoad() { super.viewDidLoad() CreateDatePicker() } // Create Date func CreateDatePicker() { datePicker.datePickerMode = .date dateInput.inputView = datePicker let toolbar = UIToolbar() toolbar.sizeToFit() let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneClicked)) toolbar.setItems([doneButton], animated: true) dateInput.inputAccessoryView = toolbar } @objc func doneClicked() { let dateFormatter = DateFormatter() dateFormatter.dateStyle = .medium dateFormatter.timeStyle = .none dateInput.text = dateFormatter.string(from: datePicker.date) self.view.endEditing(true) } @IBAction func GetDateButton(_ sender: Any) { self.GetDate = dateInput.text! performSegue(withIdentifier: "toTime", sender: self) Calendar.current.dateComponents([.day], from: self.GetDate,to: datePicker.date).day! } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let vc = segue.destination as! timeDiffr vc.textDisplay = self.GetDate } } 

Second view

import Foundation import UIKit class timeDiffr: UIViewController { @IBOutlet weak var labeltext: UILabel! var textDisplay = "" override func viewDidLoad() { super.viewDidLoad() labeltext.text = textDisplay } } 
7
  • It's best to keep your question focused to one issue. Please update your question so it is only about the error trying to use dateComponents. Commented Sep 27, 2019 at 3:14
  • You need a Date for the from parameter. You are trying to give it a String. Commented Sep 27, 2019 at 3:15
  • @rmaddy how can someone figure an issue without seeing the whole picture? Also, the issue is only with this line as im struggling to find a fix. I noticed im passing string, but im lost on which date to pass. Even with datePicker is not working. Commented Sep 27, 2019 at 3:22
  • Calendar.current.dateComponents([.day], from: self.GetDate,to: datePicker.date).day! , please notice that from should be an instance of Date, not a String @MustafaAljaburi Commented Sep 27, 2019 at 3:24
  • @MustafaAljaburi Instead of passing the string, you can convert that string into date and pass. dateComponents method takes 2 dates, so you need to pass a Date instead of string Commented Sep 27, 2019 at 3:25

1 Answer 1

1

You should notice about your coding convention

  • Variable, function names should be in camel case: GetDateshoud be getDate, GetDateButton should be getDateButton, CreateDatePicker should be createDatePicker

And your getDate is a string while Calendar.current.dateComponents([.day], from: self.GetDate,to: datePicker.date).day! return an int value. That's the problem you need to fix.

You GetDateButton function should be:

@IBAction func GetDateButton(_ sender: Any) { // Get distance by day (intValue) let distanceInDay = Calendar.current.dateComponents([.day], from: Date(), to: datePicker.date).day! } 
Sign up to request clarification or add additional context in comments.

4 Comments

About the camelCase. This is just like a playground im testing this specific problem with and i need to find a fix before implementing the solution to the final project which will be different for sure. and for the getDate, i noticed im passing a string, but even with " Calendar.current.dateComponents([.day], from: datePicker,to: Date).day!" is not working. Im sorry, but im just practicing and learning swift.
Calendar.current.dateComponents([.day], from: self.GetDate,to: datePicker.date).day! , please notice that from should be an instance of Date, not a String @MustafaAljaburi
BTW, no problem for sure, just wanna help you to have a good coding convention from beginning :) @MustafaAljaburi
I have updated my answer. You can get the distance in day (intValue) like that, if you wan a string then convert distanceInDay to string (e.g: let stringValue = String(distanceInDay) @MustafaAljaburi

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.