具有5分钟间隔获取日期的UIDatePicker以

时间:2017-01-08 18:53:46

标签: ios swift uidatepicker

我花了大约3个小时试图让UIDatePicker做我想做的事。我将其设置为UITextField的操作,以使用UIDatePicker转换为新的视图控制器。这一切都有效。我已经能够选择一个日期并将其发送回原始视图控制器。

问题是我使用5分钟的时间间隔。如果我不移动日期选择器“wheels”并选择我的完成按钮以返回到父控制器,则返回非间隔时间。因此,当我需要11:40时,11:37返回11:37。但是,如果我更改“wheels”,它将返回舍入时间。日期选择器当前显示出现的5分钟间隔。有什么线索可以引导我找到合适的解决方案吗?我在viewDidAppear的setDate上尝试了minuteIntervalUIDatePicker无济于事。以下是我一直使用的代码:

protocol DatePickerDelegate {
    func datePickerDidSelect(selectedDate: String)
}

class DatePickerVC: UIViewController  {

@IBOutlet weak var pickerView: UIDatePicker!

var selectedDate = ""
var delegate : DatePickerDelegate?


override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewDidAppear(_ animated: Bool) {
    //setDate()
    //Have tried using setDate and minuteInterval here
}

func formatDate() -> DateFormatter {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MMM d, yyyy h:mm a"
    return dateFormatter
}

func setDate() {
    let dateFormatter = formatDate()
    selectedDate = dateFormatter.string(from: pickerView.date)
    self.delegate?.datePickerDidSelect(selectedDate: selectedDate)
    print(selectedDate)
}

@IBAction func datePickerChanged(_ sender: Any) {
    setDate()
}

@IBAction func doneBtnPressed(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

@IBAction func cancelBtnPressed(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}
}

2 个答案:

答案 0 :(得分:2)

您可以查看日期并根据需要将其四舍五入:

override func viewDidLoad() {
    super.viewDidLoad()

    let calendar = Calendar.current
    var dateComponents = calendar.dateComponents([.month, .day, .year, .hour, .minute], from: datePicker.date)
    guard var hour = dateComponents.hour, var minute = dateComponents.minute else {
        print("something went wrong")
        return
    }

    let intervalRemainder = minute % datePicker.minuteInterval
    if intervalRemainder > 0 {
        // need to correct the date
        minute += datePicker.minuteInterval - intervalRemainder
        if minute >= 60 {
            hour += 1
            minute -= 60
        }

        // update datecomponents
        dateComponents.hour = hour
        dateComponents.minute = minute

        // get the corrected date
        guard let roundedDate = calendar.date(from: dateComponents) else {
            print("something went wrong")
            return
        }

        // update the datepicker
        datePicker.date = roundedDate
    }
}

随时可以询问是否有任何不清楚的地方!

答案 1 :(得分:0)

另一种替代方法是创建一个扩展名,使您可以调整初始化UIDatePicker时设置的日期值的时间间隔。

extension Date {
    var nearest30Min: Date {
        var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: self)
        guard let hours = dateComponents.hour else { return self }

        switch dateComponents.minute ?? 0 {
        case 0...14:
            dateComponents.minute = 0
        case 15...44:
            dateComponents.minute = 30
        case 44...59:
            dateComponents.minute = 0
            dateComponents.hour = hours + 1
        default:
            break
        }

        return dateComponents.date ?? self
    }
}

UIDatePicker初始化后,可以使用以下代码更新datePicker's值:

datePicker.setDate(datePicker.date.nearest30min, animated: false)