UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

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

2      

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

2      

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

2      

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

3      

@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 😄.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.