从现在起测量时间间隔

发布于 2024-11-09 23:21:08 字数 206 浏览 0 评论 0原文

任何人都知道或可以提供一些与“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的故事 2024-11-16 23:21:08

实际上,要回答您原来的问题,myles,您可以使用 timeIntervalSinceNow 。
在下面的语句中,inputDate已初始化为NSDate并设置为某个日期(您可以尝试[NSDate *inputDate = [NSDate date];< /code> 将日期设置为当前日期和时间。

NSTimeInterval timeToAlert =[inputDate timeIntervalSinceNow];

下一行是将 NSTimeInterval 放入字符串中的方法。

NSMutableString *timeinterval = [NSMutableString string];
[timeinterval appendFormat:@"%f",timeToAlert];

最后,应用程序委托类通常可以编写代码来处理。进入和退出后台祝你好运!

Actually, to answer your original question, myles, you can use timeIntervalSinceNow.
In the statement below, inputDate has been initialized as an NSDate and set to some date (you could just try [NSDate *inputDate = [NSDate date]; to set the date at the current date and time.

NSTimeInterval timeToAlert =[inputDate timeIntervalSinceNow];

The next line is a way to put that NSTimeInterval into a string.

NSMutableString *timeinterval = [NSMutableString string];
[timeinterval appendFormat:@"%f",timeToAlert];

Finally, the app delegate class is typically where code can be written to handle coming in and out of background. Good luck!

ˇ宁静的妩媚 2024-11-16 23:21:08

timeIntervalSinceNow 告诉您 NSDate 与当前时间的偏移量。您需要timeIntervalSinceDate:

NSDate *appEnteredForeground = ...;
NSDate *appEnteredBackground = ...;

NSTimeInterval difference = [appEnteredBackground timeIntervalSinceDate: appEnteredForeground];

timeIntervalSinceNow tells you the offset of an NSDate from the current time. You want timeIntervalSinceDate::

NSDate *appEnteredForeground = ...;
NSDate *appEnteredBackground = ...;

NSTimeInterval difference = [appEnteredBackground timeIntervalSinceDate: appEnteredForeground];
夜还是长夜 2024-11-16 23:21:08

您可以使用 timeIntervalSinceDate: 方法计算两个日期之间的差异:

//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 只是 double 的 typedef,以秒为单位。 [NSDate date] 使用当前日期和时间实例化一个 NSDate 对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文