日期和时间分开,晚了一天?

时间:2018-09-24 17:56:42

标签: swift date datepicker swift4

我正在尝试以12小时格式在我的应用中实施日期和时间的单独选择。但是由于某种原因,我无法在初次选择后选择当前日期,即我在开始时设置了当前日期,并且标签分别包含了时间和日期。最初设置的当前日期和时间是正确的,但是稍后当我将日期更改为上一个日期并在此之后选择当前日期时,即使设置了当前区域设置,格式化日期也要晚一天。

日期和时间的初始设置如下:

func setDateandTime() -> SubmitReportDate{

    let cell = tableView.dequeueReusableCell(withIdentifier: "submitReportDate") as! SubmitReportDate
    let dateFormatter : DateFormatter = DateFormatter()
    dateFormatter.locale = NSLocale.current
    dateFormatter.dateFormat = "MM-dd-yyyy"
    let date = Date()
    if(userInstance.userData?.Date != nil){
        self.date = (userInstance.userData?.Date)!
    }else{
        self.date = dateFormatter.string(from: date)
    }
    cell.dateLabel.setTitle(self.date, for: .normal)
    let timeFormatter : DateFormatter = DateFormatter()
    timeFormatter.locale = NSLocale.current
    timeFormatter.dateFormat = "hh:mm a"
    timeFormatter.amSymbol = "AM"
    timeFormatter.pmSymbol = "PM"
    if(userInstance.userData?.Time != nil){
        self.time = (userInstance.userData?.Time)!
    }else{
        self.time = timeFormatter.string(from: date)
    }
    cell.timeLabel.setTitle(self.time, for: .normal)
    cell.dateLabel.addTarget(self, action: #selector(dateChanged(sender:)), for: .touchUpInside)
    cell.timeLabel.addTarget(self, action: #selector(timeChanged(sender:)), for: .touchUpInside)
    return cell

}

此处正确显示了格式化后的日期和时间。 然后要更改日期,此功能就是我用的。

@objc func dateChanged(sender: UIButton){

    let alert = UIAlertController(style: .actionSheet, title: "Select date")
    alert.addDatePicker(mode: .date, date: Date(), minimumDate: nil , maximumDate: Date()) { date in
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy"
        dateFormatter.locale = NSLocale.current
        self.date = dateFormatter.string(from: date)
        self.userInstance.userData?.Date = self.date
        self.tableView.reloadRows(at: [IndexPath(item: 6, section: 0)], with: .none)
    }
    alert.addAction(title: "OK", style: .cancel)
    alert.show()

}

这是我更改时间的地方:

@objc func timeChanged(sender: UIButton){
    var maxDate = Date()
    if (userInstance.userData?.Date != nil) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy" //Your date format
        dateFormatter.locale = NSLocale.current
        //dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Current time zone
        let date = dateFormatter.date(from: ((userInstance.userData?.Date)!))
        if(date! >= maxDate){
            maxDate = date!
            let alert = UIAlertController(style: .actionSheet, title: "Select Time")
            alert.addDatePicker(mode: .time, date: maxDate, minimumDate: nil , maximumDate: maxDate) { date in
                let timeFormatter = DateFormatter()
                timeFormatter.dateFormat = "hh:mm a"
                timeFormatter.amSymbol = "AM"
                timeFormatter.pmSymbol = "PM"
                self.time = timeFormatter.string(from: date)
                self.userInstance.userData?.Time = self.time
                self.tableView.reloadRows(at: [IndexPath(item: 6, section: 0)], with: .none)
            }
            alert.addAction(title: "OK", style: .cancel)
            alert.show()
        }else{
            let alert = UIAlertController(style: .actionSheet, title: "Select Time")
            alert.addDatePicker(mode: .time, date: maxDate, minimumDate: nil , maximumDate: nil) { date in
                let timeFormatter = DateFormatter()
                timeFormatter.dateFormat = "hh:mm a"
                timeFormatter.amSymbol = "AM"
                timeFormatter.pmSymbol = "PM"
                self.time = timeFormatter.string(from: date)
                self.userInstance.userData?.Time = self.time
                self.tableView.reloadRows(at: [IndexPath(item: 6, section: 0)], with: .none)
            }
            alert.addAction(title: "OK", style: .cancel)
            alert.show()
        }

    }else{
        let alert = UIAlertController(style: .actionSheet, title: "Select Time")
        alert.addDatePicker(mode: .time, date: maxDate, minimumDate: nil , maximumDate: maxDate) { date in
            let timeFormatter = DateFormatter()
            timeFormatter.dateFormat = "hh:mm a"
            timeFormatter.amSymbol = "AM"
            timeFormatter.pmSymbol = "PM"
            self.time = timeFormatter.string(from: date)
            self.userInstance.userData?.Time = self.time
            self.tableView.reloadRows(at: [IndexPath(item: 6, section: 0)], with: .none)
        }
        alert.addAction(title: "OK", style: .cancel)
        alert.show()
    }
}

在后两个功能中,日期和时间设置错误。该代码的整个逻辑是,如果将日期设置为等于或大于当前日期,并且将其设置为小于当前日期,则时间不受限制。

1 个答案:

答案 0 :(得分:1)

您的时间更新代码也应将self.date更新为相应的值。

另外,我建议拆分您的代码以进行日期选择器工作-结果将更具可读性,因此更易于理解和调试。