从现在起测量时间间隔
任何人都知道或可以提供一些与“timeIntervalSinceNow”方法相关的示例代码...
我需要类似... time2(当应用程序进入前台时) - time1(当应用程序进入后台时)= time3(时间差)...这样我就可以使用这个数字(首选以秒为单位)来计算应用程序处于后台时我损失的时间!
我正在尝试创建日期对象、接收对象并在标签中显示/使用......
anyone know or can provide some example code relating to "timeIntervalSinceNow" method...
I need something like... time2(when app eneters foreground) - time1(when app enters background) = time3(the difference in times)... this is so i can use this number(pref in seconds) to calculate the time i have lost while the app has been in background !!
I am having trying trying to create the date objects, receive the object and display/use in a label....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,要回答您原来的问题,myles,您可以使用 timeIntervalSinceNow 。
在下面的语句中,
inputDate
已初始化为NSDate
并设置为某个日期(您可以尝试[NSDate *inputDate = [NSDate date];< /code> 将日期设置为当前日期和时间。
下一行是将
NSTimeInterval
放入字符串中的方法。最后,应用程序委托类通常可以编写代码来处理。进入和退出后台祝你好运!
Actually, to answer your original question, myles, you can use
timeIntervalSinceNow
.In the statement below,
inputDate
has been initialized as anNSDate
and set to some date (you could just try[NSDate *inputDate = [NSDate date];
to set the date at the current date and time.The next line is a way to put that
NSTimeInterval
into a string.Finally, the app delegate class is typically where code can be written to handle coming in and out of background. Good luck!
timeIntervalSinceNow
告诉您NSDate
与当前时间的偏移量。您需要timeIntervalSinceDate:
:timeIntervalSinceNow
tells you the offset of anNSDate
from the current time. You wanttimeIntervalSinceDate:
:您可以使用
timeIntervalSinceDate:
方法计算两个日期之间的差异:NSTimeInterval
只是double
的 typedef,以秒为单位。[NSDate date]
使用当前日期和时间实例化一个NSDate
对象。You can calculate the difference between two dates with the
timeIntervalSinceDate:
method:NSTimeInterval
is simply a typedef fordouble
, it's measured in seconds.[NSDate date]
instantiates anNSDate
object with the current date and time.