在给定时间(例如晚上8点)之后有一天的时间在NSArray中找到NSDate? [英] finding NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

查看:59
本文介绍了在给定时间(例如晚上8点)之后有一天的时间在NSArray中找到NSDate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,有没有一种快速的方法来标识在给定时间(例如晚上8点)之后有一天的时间的NSArray中的NSDate?

Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

除了手动遍历数组中的每个NSDate然后使用NSDateComponents来打破小时/分钟/秒之外,我什么也看不见……甚至不确定是否有一种简单的方法可以从中获取时间NSDate以代表24小时的方式表示,因为这可能会有所帮助. (例如,在这种情况下,下午6点将是18/24 = 0.75)

I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case)

推荐答案

无需中断NSDateComponents.


NSTimeInterval interval = [date1 timeIntervalSinceDate:date2];
if (interval > 0) {
    // date2 is earlier
} else {
    // date1 is earlier
}

现在,您可以用date2表示目标时间(例如,晚上8点),然后将所有数组日期与该日期进行比较.

Now you can represent your target time(8 P.M., for example) with date2 and compare all dates of array with that.

这篇关于在给定时间(例如晚上8点)之后有一天的时间在NSArray中找到NSDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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