任何人都知道或可以提供一些与“timeIntervalSinceNow”方法相关的示例代码......
我需要类似... time2(当应用程序进入前台时)-time1(当应用程序进入后台时)= time3(时间差)...这样我就可以使用这个数字(以秒为单位)来计算时间我在应用程序处于后台时迷路了!
我正在尝试创建日期对象、接收对象并在标签中显示/使用....
任何人都知道或可以提供一些与“timeIntervalSinceNow”方法相关的示例代码......
我需要类似... time2(当应用程序进入前台时)-time1(当应用程序进入后台时)= time3(时间差)...这样我就可以使用这个数字(以秒为单位)来计算时间我在应用程序处于后台时迷路了!
我正在尝试创建日期对象、接收对象并在标签中显示/使用....
实际上,要回答您的原始问题,迈尔斯,您可以使用timeIntervalSinceNow
. 在下面的语句中,inputDate
已初始化为一个NSDate
并设置为某个日期(您可以尝试[NSDate *inputDate = [NSDate date];
将日期设置为当前日期和时间。
NSTimeInterval timeToAlert =[inputDate timeIntervalSinceNow];
下一行是将其NSTimeInterval
放入字符串中的一种方法。
NSMutableString *timeinterval = [NSMutableString string];
[timeinterval appendFormat:@"%f",timeToAlert];
最后,应用程序委托类通常是可以编写代码来处理进出后台的地方。祝你好运!
You can calculate the difference between two dates with the timeIntervalSinceDate:
method:
//When app enters background:
self.backgroundDate = [NSDate date]; //this should be a property
//...
//When the app comes back to the foreground:
NSTimeInterval timeSpentInBackground = [[NSDate date] timeIntervalSinceDate:self.backgroundDate];
NSTimeInterval
is simply a typedef for double
, it's measured in seconds. [NSDate date]
instantiates an NSDate
object with the current date and time.
timeIntervalSinceNow
告诉你NSDate
从当前时间的偏移量。你想要timeIntervalSinceDate:
:
NSDate *appEnteredForeground = ...;
NSDate *appEnteredBackground = ...;
NSTimeInterval difference = [appEnteredBackground timeIntervalSinceDate: appEnteredForeground];