is there a way to fill in the forward slashes in a birthdate automatically with Swift

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

let DOBtext = textField.text!

if DOBtext.count > 1 {

textField.text = "\(textField.text!)/\(string)"

return false

}

}


this result gives me: "10/2/0/2/1/9/8/9"


I am trying to get : "10/20/1989"


thank you

What calendar localization are you employing for time ~ date formatting?

let dobInput = DOB.text
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"

Setting a date format string like this is a really bad idea. Even within the English-speaking world, everyone outside of North America will hate you forever if you force them to use the wacky month-first US date format (-:

In most cases you’ll want to set up the date formatter via one or both of its

dateStyle
and
timeStyle
properties. For example:
let df = DateFormatter()
df.dateStyle = .short
print(df.string(from: Date()))
// Prints "04/12/2017", on my UK English system today.

However, if you’re inputting dates on iOS it might be better to use

UIDatePicker
. In general entering text is a bit of a challenge on iOS, so using something like the date picker makes for a better UI.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
is there a way to fill in the forward slashes in a birthdate automatically with Swift
 
 
Q