开发者

Calculating time 90 minutes prior to due date/time in Objective C (iPhone app)

开发者 https://www.devze.com 2023-02-24 09:02 出处:网络
This is a completely noobish question, but I spent 2 hours yesterday trying to make it work, and I\'m obviously missing something very basic.

This is a completely noobish question, but I spent 2 hours yesterday trying to make it work, and I'm obviously missing something very basic.

What I need to do is take开发者_StackOverflow中文版 input from user of date/time and count back 90 minutes for an alert.

Could someone please post an example calculation, where you have a var that holds user input and a new var that receives the result of this computation? (all done in Objective C for use in an iPhone app) Thank you!


I suspect you could do something like:

NSDate *alertDate = [userDate dateByAddingTimeInterval:-5400.0];


I think this should work:

NSDate * alarmDate = [NSDate dateWithTimeInterval:5400 sinceDate:userDefinedDate];
NSDate * now = [NSDate date];   
NSTimeInterval wait = [now timeIntervalSinceDate:alarmDate];

[self performSelector:@selector(callAlarm) withObject:nil afterDelay:fabs(wait)];

Although I do agree with Nick too, adding your work its much more productive..


Assuming you have a UIDatePicker, your target date will already be in an NSDate object. If it's coming from another source, you're probably ending up with it in an NSDate object, either from a string via an NSDateFormatter or by some other means.

From an NSDate object, you can get an NSTimeInterval relative to some absolute date. That's a C primitive type (it's a double in practice, but obviously don't code to depend on that) that you can do arithmetic directly on. So you can subtract 90 minutes directly from that. There are then various + dateWithTimeInterval... class methods on NSDate that will allow you to get a date from the result.

0

精彩评论

暂无评论...
验证码 换一张
取 消