How To Get Day Before And After In Swift

Working with dates in Swift doesn’t have to be hard!

How To Get Day Before And After In Swift

Create an extension to Date:

extension Date {
    var dayAfter: Date {
        return Calendar.current.date(byAdding: .day, value: 1, to: self)!
    }

    var dayBefore: Date {
        return Calendar.current.date(byAdding: .day, value: -1, to: self)!
    }
}

Now you can access these variables like so:

let tomorrow = Date().dayAfter
let yesterday = Date().dayBefore
If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts