How to take timezone into account in Calendar.current.isDateInToday?

Viewed 533

I use an API that returns a date string such as 2021-05-05T23:00:00+00:00. Then I convert this to a Swift Date object and I'm trying to use Calendar.current to check if the date is today or tomorrow.

    let apiDate = "2021-05-05T23:00:00+00:00";
    let dateFormatter = ISO8601DateFormatter()
    let myDate = dateFormatter.date(from: apiDate)!
    
    if Calendar.current.isDateInToday(myDate) {
         print("Date is today") // this should be true
    } else if Calendar.current.isDateInTomorrow(myDate) {
         print("Date is tomorrow") // this is true instead
    } else {
         print("Date is not today or tomorrow")
    }

The problem is that isDateInToday should be true. For some reason, it adds my timezone (+3 hours) when checking if it's today and that returns false. How can I make it so that I get the result based on the phone local time?

0 Answers
Related