【问题标题】:How can i check my current time is between starttime and endtime? [duplicate]如何检查我的当前时间是否在开始时间和结束时间之间? [复制]
【发布时间】:2013-01-06 16:00:55
【问题描述】:

可能重复:
How to Check if an NSDate occurs between two other NSDates

在我的应用程序中,我有这样的字符串时间,

开始时间(字符串):上午 10:00

结束时间(字符串):下午 12:22

我现在的时间是上午 11.55

我如何检查我的当前时间是否在开始时间和结束时间之间?

【问题讨论】:

标签: iphone objective-c cocoa-touch ios5 ios4


【解决方案1】:

这样使用:

首先使用NSDateFormatter 将您的字符串转换为NSDate

然后直接将这两个日期与 currentDate 进行比较,currentDate 始终为[NSDate date]

你可以使用这两个NSDate的实例方法。

isGreaterThan:isLessThan:

或者,

if (! (([currentDate compare:startDate]==NSOrderedAscending) && ([currentDate compare:endDate]==NSOrderedDescending)){
    NSLog(@"Valid date");
}

【讨论】:

    【解决方案2】:

    首先尝试删除 AM 和 PM

    喜欢它的 PM,然后在该时间范围内加上 +12。

    然后检查天气当前时间在“:”之前的数字大于开始时间,小于结束时间..如果返回true,那么您可以检查“:”之后的数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多