从手动datePicker获取时间根据所选日期swift

时间:2016-03-22 05:21:38

标签: ios swift datepicker time-format

enter image description here我有两个字段叫日期和时间。我正在使用此代码创建datepicker

var selectedDate : NSDate!
func date_picker2(textField: UITextField)
    {
        var toolbar1:UIToolbar = UIToolbar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 40))
        var done:UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target:self, action: Selector("cancelBtnClicked1:"))
        done.tag  = 203
        var cancel:UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("cancelBtnClicked1:"))

        cancel.tag = 202


   var space:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    toolbar1.items = [cancel,space,done]
    toolbar1.barTintColor = UIColor(red: 23.0/255.0, green: 174.0/255.0, blue: 202.0/255.0, alpha: 1)
    toolbar1.tintColor = UIColor.whiteColor()
    txt_Dob.inputAccessoryView = toolbar1
    txt_birthTime.inputAccessoryView = toolbar1

    datePicker = UIDatePicker(frame: CGRectMake(toolbar1.frame.size.width - 65, 0, 0, 300))
    let currentCalendar = NSCalendar.currentCalendar()

    let dateComponents = NSDateComponents()
    dateComponents.year = -80
    datePicker.maximumDate = NSDate()


    if txt_Dob.isFirstResponder()
    {
        datePicker.datePickerMode = UIDatePickerMode.Date
    }
    if txt_birthTime.isFirstResponder()
    {
        datePicker.datePickerMode = UIDatePickerMode.Time
        println("selectedDate: \(selectedDate)")
        if selectedDate == NSDate(){
            datePicker.maximumDate = NSDate(timeIntervalSinceNow:0)
        }
    }

    datePicker.timeZone = NSTimeZone.localTimeZone()
    datePicker.locale = NSLocale.currentLocale()
    datePicker.calendar = NSCalendar.currentCalendar()
    datePicker.backgroundColor = UIColor.whiteColor()
    txt_Dob.inputView = datePicker
    txt_birthTime.inputView = datePicker
}

func cancelBtnClicked1(sender:UIButton!)
{
    var btnTag = sender.tag

    if btnTag == 203
    {
        if( txt_Dob.isFirstResponder())
        {
            if sender.tag == 203
            {
                var dateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "dd-MM-yyyy"
                txt_Dob.text = dateFormatter.stringFromDate(datePicker.date)

            }
        }
        else if( txt_birthTime.isFirstResponder())
        {
            if sender.tag == 203
            {
                var dateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "HH:mm"
                txt_birthTime.text = dateFormatter.stringFromDate(datePicker.date)
               }
        }
        txt_Dob.resignFirstResponder()
        txt_birthTime.resignFirstResponder()
    }
    else
    {
        self.view.endEditing(true)
    }
}

现在我想要如果我今天选择的日期选择器应该允许我花时间到现在,但是如果我将在前天选择日期,它应该允许我花费任何时间。

我没有得到如何做到这一点。谁能建议我怎么做?

谢谢..

1 个答案:

答案 0 :(得分:2)

只需更新时间戳的最大日期并匹配当前日期,例如:

dateComponents.hour = 23
dateComponents.minute = 59
dateComponents.second = 59

datePicker.maximumDate = currentCalendar?.dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions())

if txt_Dob.isFirstResponder()
{
    datePicker.datePickerMode = UIDatePickerMode.Date
}
if txt_birthTime.isFirstResponder()
{
    datePicker.datePickerMode = UIDatePickerMode.Time
    if selectedDate == NSDate(){
    datePicker.maximumDate = NSDate(timeIntervalSinceNow:0)
    }
}

当用户从txt_Dob日期选择器中选择日期时,您必须更新selectedDate。

if( txt_Dob.isFirstResponder())
    {
        if sender.tag == 203
        {
            var dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd-MM-yyyy"
            txt_Dob.text = dateFormatter.stringFromDate(datePicker.date)
            selectedDate = datePicker.date
            txt_birthTime.text = ""
        }
    }