-1

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.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Pengu
  • 19
  • 4

1 Answers1

1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You might say: well, surely 5 minutes before 3:01 AM is 2:56 AM, so I don't need a full date. But you'd be wrong; I can tell you a circumstance under which that would not be true, and we need the date in order to know that circumstance. There is a day each year, where I live, when 5 minutes before 3:01AM is 1:56AM! Calendars are complicated things; the only way to work with them correctly is through actual dates. – matt May 08 '19 at 14:30