The expected number of months for the below code should be -48 months. It used to work like this Until iOS17. Now when building with iOS 18 it gives -47 months.
Changing the two dates with one day back works as expected
import Foundation
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = .gmt
let components1 = DateComponents(
calendar: calendar,
year: 2004,
month: 2,
day: 29 //28 in case of changing day to 28 it works as expected
)
guard let date1 = components1.date else {
exit(1)
}
let components2 = DateComponents(
calendar: calendar,
year: 2008,
month: 2,
day: 29 //28 in case of changing day to 28 it works as expected
)
guard let date2 = components2.date else {
exit(1)
}
print(date1)
print(date2)
let months = calendar.dateComponents([.month, .isLeapMonth], from: date2, to: date1)
print(months)
You should file a bug about this. Foundation has been undergoing major refactoring over the last few years [1] and it looks like you’ve found something that it broke.
Please post your bug number, just for the record.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] If you’re curious, watch the first half of Swift & Interoperability.
https://youtu.be/**6C_XEv1Mo