Дата не будет следовать за часовым поясом

Привет я пытаюсь реализовать уведомление о дате / времени, но дата 2 часа выходной.

Код при выборе даты

func SelectTime(sender: UIView){

    formatter.timeZone = TimeZone.autoupdatingCurrent
    let timePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.time, selectedDate: userDate, doneBlock: {
        picker, userDateWithTime, index in

        self.formatter.timeZone = TimeZone.autoupdatingCurrent

        self.formatter.dateFormat = "yyyy MMMM dd, HH:mm"
        self.dateSelected.text = self.formatter.string(for: userDateWithTime)

        //print("User date picked \(self.formatter.string(from: userDateWithTime as! Date))")
        return

Код для создания Уведомления

let uuid = UUID().uuidString
    let notification = UNMutableNotificationContent()
    notification.title = "Plus - Todo"
    //notification.subtitle
    notification.body = taskText.text!

    let calendar = Calendar.current
    let dateComponents = calendar.dateComponents(in: .current, from: userDateWithTime)

    //let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: correctDate!)


    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
    //let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: uuid, content: notification, trigger: notificationTrigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

1 ответ

Проблема была в том, что userPickedDateWithTime имел тип Any, а не Date. Я решил это, создав formattedDate с типом date и установив formattedDate = userPickedDateWithTime как! Дата, а затем использовал formattedDate вместо userPickedDateWithTime при создании уведомления

var formattedDate = Date()
func SelectTime(sender: UIView){

formatter.timeZone = TimeZone.autoupdatingCurrent
let timePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.time, selectedDate: userDate, doneBlock: {
    picker, userDateWithTime, index in

    self.formatter.timeZone = TimeZone.autoupdatingCurrent

    self.formatter.dateFormat = "yyyy MMMM dd, HH:mm"
    self.dateSelected.text = self.formatter.string(for: userDateWithTime)

    //print("User date picked \(self.formatter.string(from: userDateWithTime as! Date))")
    formattedDate = userPickedDateWithtime as! Date // <-- This solved the problem

    return

Код уведомления

let uuid = UUID().uuidString
let notification = UNMutableNotificationContent()
notification.title = "Plus - Todo"
//notification.subtitle
notification.body = taskText.text!

let calendar = Calendar.current
let dateComponents = calendar.dateComponents(in: .current, from: formattedDate)

//let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: correctDate!)


let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
//let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: uuid, content: notification, trigger: notificationTrigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Другие вопросы по тегам