Reputation: 457
I have a
@IBOutlet weak var EndDatePicker: UIDatePicker?
what I want to is, If a string called date
is nil, set the datePicker to Today, the "D-Day":
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
if(task?.date != nil){
let date = dateFormatter.dateFromString(task!.date!)
EndDatePicker!.date = date!
}else{
//EndDatePicker!.date = D-Day?
}
Thank you for your help, I don't find on internet the way to do this on swift.
Regards.
Upvotes: 0
Views: 141
Reputation: 285280
Since Swift 1.2 you can perform two optional binding checks in the same line.
The first check is if date
is nil, the second if the dateformatter
could create a valid date
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let dateString = task?.date, date = dateFormatter.dateFromString(dateString) {
EndDatePicker.date = date
}
else {
EndDatePicker.date = NSDate()
}
Upvotes: 1
Reputation: 777
try this :
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.stringFromDate(NSDate())
let text = dateString
Upvotes: 2