Reputation: 78
In my code below i have implemented a date picker to be the subview of my textfield. I am stuck on the function; 'verifyAge'. The if statement in the function is working fine and will change the textField colour when the Date Picker date is above or below the specified date. But it only works to the corresponding year But does not Work to the exact Day. I have tried searching for the past two days and couldn't find a solution to my exact problem.
class AgeConfirmViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var txtAgeConfirmation: UITextField!
let datePicker: UIDatePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
txtAgeConfirmation.delegate = self
createDatePicker()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
txtAgeConfirmation.inputView = datePicker
datePicker.addTarget(self, action: #selector(self.datePickerChanged(sender:)), for: .valueChanged)
}
func datePickerChanged(sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.timeStyle = .none
formatter.dateStyle = .long
txtAgeConfirmation.text = formatter.string(from: sender.date)
verifyAge()
}
func createDatePicker() {
datePicker.datePickerMode = .date
datePicker.maximumDate = Calendar.current.date(byAdding: .year, value: 0, to: Date())
}
func verifyAge() {
let dateOfBirth = datePicker.date
let today = Date()
let gregorian = NSCalendar(identifier: NSCalendar.Identifier.gregorian)
let age = gregorian?.components([.month, .day, .year], from: dateOfBirth, to: today, options:[])
if (age?.year)! < 21 {
txtAgeConfirmation.textColor = UIColor.red
} else if (age?.year)! > 21 {
txtAgeConfirmation.textColor = UIColor.black
}
}
Upvotes: 0
Views: 3341
Reputation: 285150
Simple solution, ignore day
and month
:
let gregorian = Calendar(identifier: .gregorian)
let ageComponents = gregorian.dateComponents([.year], from: dateOfBirth, to: Date())
let age = ageComponents.year!
txtAgeConfirmation.textColor = age < 21 ? .red : .black
Or if you want to ignore the time
let ageComponents = calendar.dateComponents([.year], from: calendar.startOfDay(for: dateOfBirth), to: calendar.startOfDay(for: Date()))
In Swift 3 there are less question and exclamation marks by using native Calendar
struct.
The age exclamation mark after year
is absolutely safe because the year
component is clearly specified.
Upvotes: 4