WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Date Comparison

Forums > Swift

I have an object with a birth date. I want to check if the month and day of the birth date are equivalent to the current day and month. I want to replicate what Twitter does for user's birthday, show balloons, but I'm having trouble comparing dates.

   

I just checked and I couldn't find anything helpful, but thanks for the reconmendation.

   

I must have nit seen it the first time. I found it.

   

Hi @Brandon-Rod

Sorry just seen this how if you doing check to see date is in today then Apple have something already (which is good idea to use as if do Manual then you have to make sure all the components match as well). Use Calendar.current.isDateInToday(date) which return a Bool

let date = Date.now

if Calendar.current.isDateInToday(date) {
    print("Today a gift that why it called present")
} else if Calendar.current.isDateInTomorrow(date) {
    print("Tomorrow is another day")
} else if Calendar.current.isDateInYesterday(date) {
    print("You can not change the past")
}

if you want to make it easier to read create a file for this extension

extension Date {
    var isToday: Bool {
        Calendar.current.isDateInToday(self)
    }

    var isTomorrow: Bool {
        Calendar.current.isDateInTomorrow(self)
    }

    var isYesterday: Bool {
        Calendar.current.isDateInYesterday(self)
    }
}

then it look like this

let date = Date.now

if date.isToday {
    print("Today a gift that why it called present")
} else if date.isTomorrow {
    print("Tomorrow is other day")
} else if date.isYesterday {
    print("You can not change the past")
}

@Obelix if you put a link put the title of the article not something else it just confusing. eg

How to check whether one date is similar to another

   

I want to check if the month and day of the birth date are equivalent to the current day and month.

The above solutions, while useful for comparing Dates in other senses, won't really do what you say you want to do; that is, determine if a month and day in a different year is the same month and day as today.

Instead, what you need is this:

extension Date {
    func isSameDayAndMonth(as date: Date) -> Bool {
        let cal = Calendar.current
        let compare = cal.dateComponents([.year, .month, .day], from: self, to: date)
        //returns how many years, months and days between self and the given date
        //year will be different, of course, 
        //but we want no difference between month and day
        return compare.month == 0 && compare.day == 0
    }
}

Usage:

import Foundation

let cal = Calendar.current

let today = Date.now //20 April 2022
var bday: Date
//different month, different day
bday = cal.date(from: DateComponents(year: 1970, month: 8, day: 17))!
print(bday.isSameDayAndMonth(as: today)) //false
//same month, different day
bday = cal.date(from: DateComponents(year: 1970, month: 4, day: 30))!
print(bday.isSameDayAndMonth(as: today))  //false
//different month, same day
bday = cal.date(from: DateComponents(year: 1970, month: 3, day: 20))!
print(bday.isSameDayAndMonth(as: today))  //false
//same month, same day
bday = cal.date(from: DateComponents(year: 1970, month: 4, day: 20))!
print(bday.isSameDayAndMonth(as: today))  //true

1      

@roosterboy this made it work better. The other method in the article worked if it was the same day, month, and year, this one works perfectly. Thanks 😄.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.