如何确定在任意NSDate期间是否发生NSTimeInterval? [英] How to determine if an NSTimeInterval occurred during an arbitrary NSDate?

查看:167
本文介绍了如何确定在任意NSDate期间是否发生NSTimeInterval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTimeInterval,我想知道该时间戳是否落在日期的开始和结束之间。基本上我需要能够做如下:

I have an NSTimeInterval, and I'd like to know whether that timestamp falls between the start and end of a date. Basically I need to be able to do something like:

NSDate *today = [NSDate date];
NSTimeInterval myInterval = someInterval;

[date returnYesIfFallsThisDate:myInterval];

有一个直接的方法来确定我的NSTimeInterval是否与NSDate对象

Is there a straight forward method to figure out if my NSTimeInterval falls on the same day as an NSDate object?

感谢所有。

推荐答案

NSTimeInterval只是一个数字的秒。要比较它与一个NSDate,你需要解释相对于一些参考点的时间间隔,然后创建一个NSDate的结果,然后比较两个NSDates。

An NSTimeInterval is just a number of seconds. To compare it to an NSDate, you need to interpret the time interval relative to some reference point, then create an NSDate with the result, then compare the two NSDates.

是两个标准参考日期: 2001-01-01 1970-01-01 (后者是UNIX时间)。如果这两者都不是正确的参考日期,请使用NSCalendar为正确的参考日期创建NSDate,然后创建您的NSDate相对于

There are two standard reference dates: 2001-01-01 and 1970-01-01 (the latter being "UNIX time"). If neither of those is the correct reference date, use NSCalendar to create an NSDate for the correct reference date, then create your NSDate of interest relative to that.

现在进行比较。

如果你关心的是日历上的日期,那么你将创建NSDate,然后使用NSCalendar以获取NSDateComponents的日期。具体来说,你会得到年,月和日。然后比较这些。

If all you care about is the date on the calendar, then you'll create the NSDate, then use an NSCalendar to get NSDateComponents for the date. Specifically, you'll get the year, month, and day-of-the-month, then compare those.

没有必要担心开始和结束时间;事实上,如果您只关心日历上的日期,则可以完全忽略时间。

There's no need to worry about "start" and "end" times; in fact, if you only care about the date on the calendar, you can ignore the time-of-day entirely.

这篇关于如何确定在任意NSDate期间是否发生NSTimeInterval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆