I want a user to enter a start date in a text field.
How do I set the default date of the text field to today's date?
Simples?
In your viewDidLoad() try this:
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .NoStyle
yourTextField.text = dateFormatter.stringFromDate(NSDate())
Refer to the Apple docs to learn more about NSDateFormatter
This should work:
override func viewDidLoad() {
super.viewDidLoad()
let todaysDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy"
let dateString = dateFormatter.string(from: todaysDate)
yourTextField.text = dateString
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.