快速将int从字符串时间4:01减至3:51

时间:2018-11-22 07:32:24

标签: ios swift datetime notifications uilocalnotification

hi,我想从字符串时间发出本地通知,我希望通知在给定时间之前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
    }
  }
}

2 个答案:

答案 0 :(得分:3)

这是您需要的吗?

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))
}

答案 1 :(得分:0)

在这里,我将解决方案与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)