malcommac / SwiftDate

🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.
MIT License
7.61k stars 767 forks source link

getInterval() weird behaviour #790

Closed zero0cool0 closed 1 year ago

zero0cool0 commented 2 years ago

IOS: 15.1 on a real device XCode: 13.1 SwiftDate: 6.3.1 - installed via CocoaPods

Trying to understand the exact behaviour of getInterval() to calculate the difference in calendar days between two DateInRegion instances. How come

       let d1 = "2021-12-04T12:00:00+01:00".toDate()!
       let d2 = "2021-12-05T06:00:00+01:00".toDate()!
       debugPrint(d1.getInterval(toDate: d2, component: .day))

outputs 1, which is what I would expect, but

       let d1 = "2021-12-04T12:00:00+01:00".toDate()!
       let d2 = "2021-12-05T00:00:00+01:00".toDate()!
       debugPrint(d1.getInterval(toDate: d2, component: .day))

outputs 0 which is unexpected, as clearly, d2 is one day after d1 ?

mhmdzaid commented 2 years ago

it's a half day (12 h) difference based on the hours in the date @zero0cool0