Removing components from a Date in Swift

RevenueCat logo
Relax, you can roll back your mobile release

No one is immune from shipping critical bugs to production, but Runway helps you limit the amount of havoc that can cause.

We recently fixed a bug in our app Helm which occurred when trying to set the time of a scheduled release using the App Store Connect API.

The date we were sending in the request contained the day, month, year, hour and minute, but the API would only allow us to be precise up to the hour.

To fix the issue as quickly as possible and as SwiftUI’s DatePicker can not be configured to only allow the user to select the hour of the day without specifying the minute, we decided to remove the extra information from the date before sending it to the API with a small extension on Date:

extension Date {
    func keepingInformation(
        dateComponents: Set<Calendar.Component> = [.year, .month, .day, .hour],
        in calendar: Calendar = .current
    ) -> Date {
        let dateComponents = calendar.dateComponents(dateComponents, from: self)
        return dateComponents) ?? self

The code above extracts the desired date components from the Date instance and then uses the provided Calendar to create a new Date just with those components.