计算从两个 UIDatePickers 中选择的日期的值 Swift 4

Calculate Value of dates selected from two UIDatePickers Swift 4

我有两个 UIDatePickers 和一个标签。我想计算两个日期选择器的开始日期和结束日期选择之间的差异,并将差异显示在标签上。这是我当前的代码:

@IBOutlet weak var YTDDateRange: UILabel!

@IBOutlet weak var startingDateMonthInput: UITextField!

@IBOutlet weak var endingDateMonthInput: UITextField!

private var datePickerStart: UIDatePicker?

private var datePickerEnd: UIDatePicker?

override func viewDidLoad() {
    super.viewDidLoad()

    datePickerStart = UIDatePicker()

    datePickerEnd = UIDatePicker()

    datePickerStart?.datePickerMode = .date

    datePickerEnd?.datePickerMode = .date

    datePickerStart?.addTarget(self, action: #selector(YTDChartViewControllerBase.dateChangedStart(datePicker:)), for: .valueChanged)

    datePickerEnd?.addTarget(self, action: #selector(YTDChartViewControllerBase.dateChangedEnd(datePicker:)), for: .valueChanged)

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(YTDChartViewControllerBase.viewTapped(gestureRecognizer:)))

    view.addGestureRecognizer(tapGesture)

    startingDateMonthInput.inputView = datePickerStart

    endingDateMonthInput.inputView = datePickerEnd

    let dateformatter = DateFormatter()
    dateformatter.dateFormat = "MM/yyyy"

    dateformatter.dateStyle = DateFormatter.Style.long
    dateformatter.timeStyle = DateFormatter.Style.none

    var calendar = Calendar(identifier: .gregorian)
    calendar.timeZone = TimeZone(identifier: "PST")!
    var startDateComponents: DateComponents = DateComponents()
    startDateComponents.year = 1928
    startDateComponents.month = 1
    let defaultDate: Date = calendar.date(from: startDateComponents as DateComponents)!

    self.datePickerEnd!.date = Date()

    self.datePickerStart!.date = defaultDate as Date
    startingDateMonthInput.text = dateformatter.string(from: datePickerStart!.date)
    endingDateMonthInput.text = dateformatter.string(from: datePickerEnd!.date)
}

@objc func viewTapped(gestureRecognizer: UITapGestureRecognizer) {

    view.endEditing(true)

}

@objc func dateChangedStart(datePicker: UIDatePicker) {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM/yyyy"
    dateFormatter.dateStyle = DateFormatter.Style.long
    startingDateMonthInput.text = dateFormatter.string(from: datePicker.date)
    view.endEditing(true)

}

@objc func dateChangedEnd(datePicker: UIDatePicker) {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM/yyyy"
    dateFormatter.dateStyle = DateFormatter.Style.long
    endingDateMonthInput.text = dateFormatter.string(from: datePicker.date)
    view.endEditing(true)

}

这就是我试图找到两个日期选择器之间的区别。

func YTDMarketClose(from datePickerStart: Date, to datePickerEnd: Date) -> Date {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM/yyyy"
    dateFormatter.dateStyle = DateFormatter.Style.long
    let YTDBegin = datePickerEnd.timeIntervalSince(datePickerStart)
    let today = Date()
    if YTDBegin < 0 {
        return today
    } else {
        return today.addingTimeInterval(YTDBegin)
        print(YTDBegin)
    }
}

但是当我尝试将它设置为文本标签时,出现错误“无法将 'Date' 的类型值分配给类型 'String?'。

 YTDDateRange.text = YTDMarketClose(from: datePickerStart!.date, to: datePickerEnd!.date)

我这样做是正确的还是我遗漏了什么。非常感谢任何帮助。

两个日期之间的差异不是另一个日期,而是一个时间间隔。您的 YTDBegin 变量是 TimerInterval 类型。这是秒数。不要试图将其转换回日期。将其转换为所需格式的字符串。

您的 YTDMarketClose 方法需要 return String,而不是 Date。然后就可以把结果赋给标签了。

您应该使用 DateComponentsFormatter 从两个日期之间的差异中获取字符串。

func ytdMarketClose(from datePickerStart: Date, to datePickerEnd: Date) -> String {
    let formatter = DateComponentsFormatter()
    // configure the formatter to suit your needs
    let result = formatter.string(from: datePickerStart, to: datePickerEnd)

    return result ?? "Unknown"
}

作为旁注,class、结构和枚举名称以大写字母开头是标准的。变量、方法和案例名称以小写字母开头。