I am looking into a bug in my birthday app. The member .day
of Date
returns the wrong day but curiously only in the timezone UTC-4. .year
and .month
are totally fine too. Are there any other ways to get the day of the month from a date reliably in any given timezone?
let birthday = Date().midnightUTC() // 2002-01-12 00:00:00 +0000
func upcomingBirthday() -> Date {
var dc = DateComponents()
dc.calendar = .current
dc.year = Date().year
dc.month = birthday.month
dc.day = birthday.day
print("Date Day: \(birthday.day)") // 10
let today = Date().midnightUTCDate()
if dc.date! == today {
return today
} else if dc.date!.midnightUTCDate().isEarlierThan(today) {
dc.year! += 1
return dc.date!
} else {
return dc.date!.midnightUTCDate()
}
}
Date
s to UTC, for example