0

I have this code:

let miesiacOd : 2017
let rokOd : Int = 10
let dzienOd : Int = 1
let dataOd = String(format: "%02d-%02d-%02d", rokOd, miesiacOd, dzienOd)

let miesiacDo : Int = 2018
let rokDo : Int = 10
let dzienDo : Int = 1
let dataDo = String(format: "%02d-%02d-%02d", rokDo, miesiacDo, dzienDo)


let dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = "yyyy-MM-dd"

I'm trying to compare it, but I have error. When converting variables to dates:

let dataDo2 = dateFormatter2.date(from: dataDo)
let dataOd2 = dateFormatter2.date(from: dataOd)

I have the date and time as a result. For example: 2017-10-01 +000 Why is this happening and how to fix it?

Finally, I would like to check if the current date is within the above dates.

I'm trying to do it like this:

let sprawdzamDostepnoscDat = Date().isBetweeen(date: dataOd2!, andDate: dataDo2!)


extension Date {

    func isBetweeen(date date1: Date, andDate date2: Date) -> Bool {
        return date1.timeIntervalSince1970 < self.timeIntervalSince1970 && date2.timeIntervalSince1970 > self.timeIntervalSince1970
    }

}

Will this solution be ok?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Łukasz Betta
  • 131
  • 3
  • 12
  • `Date` is always a point in time, it always includes both day and time components. You should be using `DateComponents` and `Calendar` to create the date, not a custom string and then parsing that custom string using a formatter. – Sulthan Apr 08 '18 at 08:04
  • The first part is answered here: https://stackoverflow.com/questions/39937019/nsdate-or-date-shows-the-wrong-time. – Martin R Apr 08 '18 at 08:05
  • For the second part, see https://stackoverflow.com/questions/32859569/check-if-date-falls-between-2-dates. – Martin R Apr 08 '18 at 08:07
  • I have originally edited your question to translate variable names to English but now I see that you were not using them correctly. You set `month` (`miesiac`) to be `2017` and `year` (`rok`) to be `10`. That cannot be correct. – Sulthan Apr 08 '18 at 08:08
  • The date format is wrong, it must be `"MM-yy-dd"` – vadian Apr 08 '18 at 08:09

1 Answers1

0

You don't need a formatter (string parser) to create Date:

var dateFromComponents = DateComponents()
dateFromComponents.year = 2017
dateFromComponents.month = 10
dateFromComponents.day = 1
let dateFrom = Calendar.current.date(from: dateFromComponents)

var dateToComponents = DateComponents()
dateToComponents.year = 2018
dateToComponents.month = 10
dateToComponents.day = 1
let dateTo = Calendar.current.date(from: dateToComponents)

Also note that Date is already comparable, therefore your inBetween function can be just:

extension Date {
    func isBetweeen(date date1: Date, andDate date2: Date) -> Bool {
       return date1 <= self && self <= date2
    }
}

However, if you want to ignore time and just compare the days, you should use:

extension Date {
    func isBetweeen(date date1: Date, andDate date2: Date) -> Bool {
        return Calendar.current.compare(date1, to: self, toGranularity: .day) != .orderedDescending
        && Calendar.current.compare(self, to: date2, toGranularity: .day) != .orderedDescending
    }
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • func isBetweeen(date date1: Date, andDate date2: Date) -> Bool { return Calendar.current.compare(date1, to: self, toGranularity: .day) != .orderedDescending && Calendar.current.compare(self, to: date2, toGranularity: .day) != .orderedDescending } - is not working. I have error: Cannot convert value of type 'ViewController' to expected argument type 'Date' – Łukasz Betta Apr 08 '18 at 08:21
  • In this function: func isBetweeen2(date date1: Date, andDate date2: Date) -> Bool { return date1 <= self && self <= date2 } - i have error: Binary operator '<=' cannot be applied to operands of type 'Date' and 'ViewController' – Łukasz Betta Apr 08 '18 at 08:22
  • @ŁukaszBetta The `isBetween` function is supposed to stay in `extension Date`, I thought that would be obvious – Sulthan Apr 08 '18 at 08:36