mojojojo47 mojojojo47 - 5 months ago 11
iOS Question

How to convert string in text field to NSDate type?

What I am trying to accomplish:

I am trying to make an enter button that checks the date in the UITextField to see if it is before the current date(any date that is today) and if the date in the UITextField is in the past, I want to throw an error/alert to tell the user to enter a date in the future--possibly a week or month from the current date.

A little background:

I made a viewController with a textfield that a user is going to enter a
date into and the way this works is the user presses on the textField and
a datePicker pops up allowing them to update the textField with the datePicker.

The problem I am running into:

This all works fine until I want to compare the date in the textField with the current date. This is because the date in the textField is a string and not a date that NSDate can recognize.

My question basically is


How do I convert the string date in the TextField into a format that NSDate can recognize?


Here is my code for my enterButton function:

@IBAction func enterButton(sender: AnyObject) {

let dateFormatter = NSDateFormatter()
var raceDate = raceDateTextField.text
let currentDate = NSDate()

raceDate = dateFormatter.stringFromDate(sender.date)

if currentDate.compare(raceDate) == NSComparisonResult.OrderedDescending {
print("Race Date is earlier than Current Date")

}
}


I'm getting the error


cannot convert value of type 'String?' to expected argument type 'NSDate' @ the line below


if currentDate.compare(raceDate) == NSComparisonResult.OrderedDescending {
print("Race Date is earlier than Current Date")

}


Here is the my code for updating my textField using a datePicker just for reference:

@IBAction func textFieldEditing(sender: UITextField) {

let datePicker:UIDatePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePicker
datePicker.addTarget(self, action: #selector(SecondViewController.datePickerValueChanged), forControlEvents: UIControlEvents.ValueChanged)

}

func datePickerValueChanged(sender:UIDatePicker) {

let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
raceDateTextField.text = dateFormatter.stringFromDate(sender.date)

}


Am I going about this wrong logically?
Any help with this will be greatly appreciated! :)




EDIT/UPDATE!



I made the changes like you guys advised and I am getting the same error at the same line.

Here is my updated code:

@IBAction func enterButton(sender: AnyObject) {

let dateFormatter = NSDateFormatter()
var raceDate = raceDateTextField.text
dateFormatter.dateFormat = "dd/MM/yyyy"
var minimumDate: NSDate? = NSDate()
raceDate = dateFormatter.stringFromDate(sender.date)

if minimumDate!.compare(raceDate) == NSComparisonResult.OrderedDescending {
print("Race Date is earlier than Current Date")

}
}


I think the (sender.date) parameter is wrong @ line:


raceDate = dateFormatter.stringFromDate(sender.date)


Right now I'm thinking I entered in the wrong parameter(sender.date) because raceDate still shows up as a string data type in the if statement.

Answer

Got it working correctly, thanks to @Amit Jagesha, @tymac, @Andrei Filip, and @Leo Dabus

Here is the end product in case anybody else has a similar problem and for future reference.

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MMM, dd, yyyy"
    let raceDate = raceDateTextField.text
    let date = dateFormatter.dateFromString(raceDate!)
    let minimumDate = NSDate()

    if minimumDate.compare(date!) == NSComparisonResult.OrderedDescending {
    }