How to Check if an NSDate occurs between two other

2019-01-01 05:25发布

I am trying to figure out whether or not the current date falls within a date range using NSDate.

For example, you can get the current date/time using NSDate:

NSDate rightNow = [NSDate date];

I would then like to use that date to check if it is in the range of 9AM - 5PM.

10条回答
浮光初槿花落
2楼-- · 2019-01-01 06:10

With Swift 4.2 and iOS 12, you can use one of the two solutions below in order to check if a date occurs between two other dates.


#1. Using DateInterval's contains(_:) method

DateInterval has a method called contains(_:). contains(_:) has the following declaration:

func contains(_ date: Date) -> Bool

Indicates whether this interval contains the given date.

The following Playground code shows how to use contains(_:) in order to check if a date occurs between two other dates:

import Foundation

let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let myDate = calendar.date(from: DateComponents(year: 2012, month: 8, day: 15))!

let dateInterval = DateInterval(start: startDate, end: endDate)
let result = dateInterval.contains(myDate)
print(result) // prints: true

#2. Using ClosedRange's contains(_:) method

ClosedRange has a method called contains(_:). contains(_:) has the following declaration:

func contains(_ element: Bound) -> Bool

Returns a Boolean value indicating whether the given element is contained within the range.

The following Playground code shows how to use contains(_:) in order to check if a date occurs between two other dates:

import Foundation

let calendar = Calendar.current
let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let myDate = calendar.date(from: DateComponents(year: 2012, month: 8, day: 15))!

let range = startDate ... endDate
let result = range.contains(myDate)
//let result = range ~= myDate // also works
print(result) // prints: true
查看更多
孤独总比滥情好
3楼-- · 2019-01-01 06:13

Continuing with Quinn's and Brock´s solutions, is very nice to subclass NSDate implementation, so it can be used everywhere like this:

-(BOOL) isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate {
    return (([self compare:beginDate] != NSOrderedAscending) && ([self compare:endDate] != NSOrderedDescending));
}

And at any part of your code you can use it as:

[myNSDate isBetweenDate:thisNSDate andDate:thatNSDate];

(myNSDate, thisNSDate and thatNSDate are of course NSDates :)

查看更多
时光乱了年华
4楼-- · 2019-01-01 06:18

A better version in Swift:

@objc public class DateRange: NSObject {
    let startDate: Date
    let endDate: Date

    init(startDate: Date, endDate: Date) {
        self.startDate = startDate
        self.endDate = endDate
    }

    @objc(containsDate:)
    func contains(_ date: Date) -> Bool {
        let startDateOrder = date.compare(startDate)
        let endDateOrder = date.compare(endDate)
        let validStartDate = startDateOrder == .orderedAscending || startDateOrder == .orderedSame
        let validEndDate = endDateOrder == .orderedDescending || endDateOrder == .orderedSame
        return validStartDate && validEndDate
    }
}
查看更多
一个人的天荒地老
5楼-- · 2019-01-01 06:19

There is better and more swifty solution for this problem.

extention Date {
    func isBetween(from startDate: Date,to endDate: Date) -> Bool {
        let result = (min(startDate, endDate) ... max(startDate, endDate)).contains(self)
        return result
    }
}

Then you can call it like this.

todayDate.isBetween(from: startDate, to: endDate)

Even you can pass date random as this extension checks which one is minimum and which one in not.

you can use it in swift 3 and above.

查看更多
登录 后发表回答