mojojojo47
mojojojo47

Reputation: 13

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.

Upvotes: 0

Views: 1708

Answers (5)

Lucas Farah
Lucas Farah

Reputation: 1102

Swift 3:

 let dateFormatter = DateFormatter()
 dateFormatter.dateFormat = "MMM, dd, yyyy"
 let raceDate = raceDateTextField.text
 let date = dateFormatter.date(from: raceDate!)
 let minimumDate = NSDate()

 if minimumDate.compare(date!) == ComparisonResult.orderedDescending {
 }

Upvotes: 1

mojojojo47
mojojojo47

Reputation: 13

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 {
    }

Upvotes: 0

Amit Jagesha シ
Amit Jagesha シ

Reputation: 1112

@IBAction func enterButton(sender: AnyObject) {

   let myDate = raceDateTextField.text
   let dateFormatter = NSDateFormatter()
   dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
   let date = dateFormatter.dateFromString(myDate)!
   dateFormatter.dateFormat = "dd/MM/yyyy"
   let dateString = dateFormatter.stringFromDate(date)

}

Upvotes: 0

Edison
Edison

Reputation: 11987

You said that you want to compare the date in the textField with the current date.

So just create and set a minimum date for the date picker to today. var minimumDate: NSDate?

For example:

if minimumDate.compare(raceDate) == NSComparisonResult.OrderedDescending {
    print("Race Date is earlier than Current Date. Are you a time traveler?")
}

Upvotes: 0

Andrei Filip
Andrei Filip

Reputation: 1143

The text that is currently in your textField is a String. You need to run that through a dateFormatter and get the date (NSDate). Afterwards you can compare it to the current date.

It's basically the same thing as when you set the text in your textField, but the other way around.

see: https://developer.apple.com/reference/foundation/nsiso8601dateformatter/1643127-datefromstring

Upvotes: 0

Related Questions