Мартин Иванов Мартин Иванов - 1 year ago 116
iOS Question

Subtract minutes from time picker value?

I have being wondering these days how can I subtract 3, 10 and 20 minutes from selected time from time picker in Swift 3. So for example when the user selects the start time of an event, to be able to receive notification 3, 10 or 20 minutes before the selected from the picker time value. I have tried

let value:TimeInterval = 1187.5
let periodComponents = startTimePicker.date.addingTimeInterval(-value)

for the 20 minutes earlier,
but I'm getting the value 2 hours behind(may be the console is showing me the GTM time only). Is it possible to schedule notification from "periodComponens" to repeat every week on the selected time on specific day of the week? Or I should use other subtracting method?

Answer Source

There is nothing wrong with your code. The xcode console just prints out GMT date time by default. If you use NSDateFormatter to convert your NSDate into NSString and print it out, the result will be as you expected

If you use UILocalNotification and wish to schedule it to fire weekly you can set its provided property repeatInterval to NSCalendarUnitWeekOfYear.

For example:

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *fireTime; // Your desired date time.

localNotif.fireDate = fireTime;
localNotif.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download