【问题标题】:Two UIDatePickers (Time mode) - How to tell if they are atleast 3 minutes apart?两个 UIDatePicker(时间模式) - 如何判断它们是否相隔至少 3 分钟?
【发布时间】:2014-02-28 18:40:29
【问题描述】:

我的项目中有两个UIDatePickers(时间模式),我想知道如何检查两个UIDatePicker 中的时间是否至少相隔3 分钟。这可能是一个简单的问题,但我还在学习中,希望你们理解。

谢谢。

【问题讨论】:

    标签: ios objective-c cocoa-touch uidatepicker


    【解决方案1】:

    从选择器中获取两个 NSDate 值。然后做:

    NSDate *date1 = // first date
    NSDate *date2 = // second date
    NSTimeInterval diff = fabs([date1 timeIntervalSinceDate:date2]);
    if (diff > 3 * 60) {
        // the difference is more than 3 minutes
    }
    

    另一种选择是:

    NSDate *date1 = // first date
    NSDate *date2 = // second date
    unsigned int unitFlags = NSMinuteCalendarUnit;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:date2  options:0];
    int minutes = [comps minute];
    if (minutes > 3) {
        // the difference is more than 3 minutes
    }
    

    【讨论】:

    • 感谢您的回答。我现在能够检测到它。
    猜你喜欢
    • 2014-03-22
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多