Check the date is in certain period.

Hi ,



There is a variable C with Date format.


And I want to figure out that variable is in the period from start Date A to end Date B. (A and B is also Date format)


For ex, if C is 1986/04/05 and if it is in 1980/04/30 ~ 1988/04/03 then 'true' returns.


Any way to do this ?😉

Accepted Reply

Are you talking about the Swift "Date" class?


developer.apple.com/reference/foundation/date


Or are you talking about a formatted date string?


Note that the "Date" class, which is derived from the Cocoa Obj-C "NSDate" class, is a date and time, represented in (floating point) seconds since a reference date/time. That is, a Date is basically a numeric quantity, and that's why it can be compared safely with "<" and ">". However, you have to be careful, because A and B also have a time that affects the comparison. The correctness of the comparison depends on where A, B and C come from, or how they are constructed.


If you're talking about a formatted date string, then it depends on the format whether strings can be successfully compared. Usually this is not possible to do safely.

Replies

Oh, I found a way.

So , I shared it. (if self answer is not legal in this forum, plz delete this thread)


From swift3, we can just compare like if (B < C) {   ... even if it is Date.


Thanks.

Are you talking about the Swift "Date" class?


developer.apple.com/reference/foundation/date


Or are you talking about a formatted date string?


Note that the "Date" class, which is derived from the Cocoa Obj-C "NSDate" class, is a date and time, represented in (floating point) seconds since a reference date/time. That is, a Date is basically a numeric quantity, and that's why it can be compared safely with "<" and ">". However, you have to be careful, because A and B also have a time that affects the comparison. The correctness of the comparison depends on where A, B and C come from, or how they are constructed.


If you're talking about a formatted date string, then it depends on the format whether strings can be successfully compared. Usually this is not possible to do safely.

However, you have to be careful, because A and B also have a time that affects the comparison. The correctness of the comparison depends on where A, B and C come from, or how they are constructed.

Right. To illustrate this in concrete terms, the mapping from a set of Gregorian date components (like 1986/04/05) to an absolute point in time depends on the time zone, so you might find that C falls between A and B for some users but not others.

In my experience virtually every easily-stated date and time problem has an obvious answer that’s wrong (for a fun time, read Your Calendrical Fallacy Is…), and finding the right answer requires you to really nail down the specification of the problem.

stcocoa, if you can explain more about the context of this issue we should be able to offer you better advice.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I mean Swift "Date" class.

Thanks for your advice. I will be careful about that.

Thanks, QuinceyMorris😁

With Foundation framework you can do it with very little code like this:


let dateInterval = DateInterval(start: startDate, end: endDate)
print("The interval contains the date:",  dateInterval.contains(testDate))


The most complicated work to make the above working was converting the dates from String to Date...


import Foundation
let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "YYYY/MM/dd"

    return formatter
} ()
if let startDate = dateFormatter.date(from: "1980/04/30"),
    let endDate = dateFormatter.date(from: "1988/04/03"),
    let testDate = dateFormatter.date(from: "1986/04/05") {
    let dateInterval = DateInterval(start: startDate, end: endDate)
    print("The interval contains the date:",  dateInterval.contains(testDate))
}
let formatter = DateFormatter() 
formatter.dateFormat = "YYYY/MM/dd"

Don’t do this. If you set a fixed-format date you have to set the locale to

en_US_POSIX
. If you don’t, weird stuff will happen in various edge cases. QA1480 NSDateFormatter and Internet Dates has the details here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"