Calculate age from birthday

Hello I am new here, can some one help me to place this right, in to the ViewController? I need one text field to import the number for exampel “1978” and a action button to print the right value out in a label.

let years: Int = 1978

extension Date {
var age: Int {
return Calendar.current.dateComponents([.year], from: self, to: Date()).year!
}
}

let myDOB = Calendar.current.date(from: DateComponents(year: years ))!

let myAge = myDOB.age // 38

func calculateAge (_ birthday: Date) -> Int {
return Calendar.current.dateComponents([.year], from: birthday, to: Date()).year!
}

print(“The Patients age is (myAge) years”)

OK, in your view controller you should place a text field, a button and a label using the storyboard editor. Then connect your text field and your label as IBOutlets, and your button as IBAction. Keeping this simple for the moment, any time the button action is received in the view controller you should take the contents of the text field and attempt to turn it into an Int. Then you can calculate the age in years. When you do come up with a reasonable value (and you should check the range is 0-120 or similar) you can display it in the label by making a string out of the value, then setting label.text to that string.

Does that get you on the right track? Give it a go.