简体   繁体   中英

Swift: How should I subtract 5 minutes from Datecomponents?

I am working on an app that notifies the user 5 minutes before the departure time. I'm using firebase as my database.

I've tried searching answers from the internet but the solutions are not working.

So here's the code in getting the departure hour and minute:

let hour = dict["DepartureHour"] as! NSNumber
let minute = dict ["DepartureMinute"] as! NSNumber

Putting it in a DateComponent:

var dateComponents = DateComponents()
dateComponents.hour = hour.intValue
dateComponents.minute = minute.intValue 

Now I don't know what to do next, like how should I subtract 5 minutes from it. Please help me.

You cannot do this based on the "departure hour" and "departure minute" alone. The only way to get a date "5 minutes before the departure time" is to start with an actual date-time representing the moment of departure. That requires that you know all calendar info: year, month, day, hour, minutes, seconds. When you know that, finding the date-time five minutes before it is trivial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM