Will Cohen - 6 months ago 36

Swift Question

if anyone wants a challenge, heres one: I need to create a function that subtracts time. What I mean is, it takes a time (ex. 7:35 PM), and then it has to subtract from that time (ex. 17 minutes). I find this difficult because obviously you cant just add and subtract regularly since time is based 60 not based 100, as well as the fact that you have to work with AM and PM. Any help or point in the right direction is appreciated.

Answer

```
let timeinterval = NSTimeInterval.abs(17 * 60) //time in seconds
let newDate = oldDate.dateByAddingTimeInterval(timeinterval)
```

EDIT 1: use - to subtract date.

EDIT 2:

```
let timeinterval = NSTimeInterval.abs(17 * 60) //time in seconds
let newDate = oldDate.dateByAddingTimeInterval(-timeinterval)
```

when you add the negative time interval and add it to the old date, your new date will be the subtracted date.