将 int 从字符串时间 4:01 递减到 3:51 in swift

decrement int from string time 4:01 to 3:51 in swift

嗨,我想从字符串时间发出本地通知,我希望通知在给定时间前 10 分钟启动。我有麻烦如何把时间从4:01变成3:51,请帮忙。这是我的代码

let a = "4:01"

func startNoftification(prayer: String) {
    let time = prayer.split(separator: ":").map { (x) -> Int in
    return Int(String(x))!
}
   let content = UNMutableNotificationContent()
   content.title = "Adzan"
   content.body = "it's time to sholat dzuhur"

   let gregorian = Calendar(identifier: .gregorian)
   var component = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
   component.hour = time[0]
   component.minute = time[1] - 10

   guard let date = gregorian.date(from: component) else { return }
   let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second], from: date)
   let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
   let request = UNNotificationRequest(identifier: "Hijrah", content: content, trigger: trigger)

   UNUserNotificationCenter.current().add(request) { (error) in
    if let err = error {
        print("Notif error:", err)
        return
    }
  }
}

这是你需要的吗?

let time = "4:01"

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm"

guard let date = dateFormatter.date(from: time) else {
    return 
} 

let targetTime = Date(timeInterval: -(10 * 60), since: date) // Change time interval to required value in seconds
let targetTimeString = dateFormatter.string(from: targetTime)
print(targetTimeString) // Prints 3:51

或者如果你的倒计时有很多时间成分,使用DateComponents

var dateComponents = DateComponents()
dateComponents.minute = -10
// Other components

if let targetTime = Calendar.current.date(byAdding: dateComponents, to: date)
    print(dateFormatter.string(from: targetTime))
}

这里我有解决方案结合 rakesha Shastri 答案,希望它可以帮助别人。

首先在 appDelegate 和你的控制器中导入 UserNotification

import UserNotification

并为 didFinishWithLaunchingWithOptions 添加代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge]) { (granted, error) in
        if let err = error {
            print("Notifications permission denied because:", err)
            return
        }

        if granted {
            print("Notifications permission granted.")
        }
    }
}

你可以创建一个函数并从 API 或任意字符串添加字符串时间参数,因为我没有使用函数所以我在 viewDidLoad

中传递了我的解决方案
let adzan = "4:01"

let content = UNMutableNotificationContent()
    content.title = "Adzan"
    content.body = "It's time for sholat"
    content.sound = UNNotificationSound.default

    // Format the date first from string time
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm"

    //addingTimeInterval is where you want to set notification before or after deciding time in this case I set it 10 minute before deciding time
    guard let date = dateFormatter.date(from: adzan)?.addingTimeInterval(-600) else { return }

    // and convert our date into dateComponents
    let triggerDate = Calendar.current.dateComponents([.hour, .minute, .second], from: date)

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request)