Add Days, Months or Years to a Current Date in Swift

In this tutorial, I will share with you a few code examples on how to work with dates in Swift.  I will use DateComponents to add more days, months, or years to the current Date object in Swift.

Let’s start with the basics. The first thing you need to know is that Swift provides several structures to work with dates, including Date()DateComponents(), and Calendar objects. We will primarily use these three to accomplish our task.

Adding Days to Current Date

To add days to the current date, You first create a Date() object to represent the current date. Then, create a DateComponents() object and set its day property to the number of days we want to add. Finally, you use the date(byAdding:to:) method of the Calendar object to calculate the future date. Here’s how you can do it:

let currentDate = Date()
var dateComponent = DateComponents()
dateComponent.day = 1

let futureDate = Calendar.current.date(byAdding: dateComponent, to: currentDate)

print(currentDate)
print(futureDate!)

This code will print the current date and the date after one day.

Output:

2024-01-14 16:30:15 +0000
2024-01-15 16:30:15 +0000

Adding Months to Current Date

Adding months to the current date follows the same pattern as adding days. You just changed the month property of the DateComponents object instead of the day property:

let currentDate = Date()
var dateComponent = DateComponents()
dateComponent.month = 2

let futureDate = Calendar.current.date(byAdding: dateComponent, to: currentDate)

print(currentDate)
print(futureDate!)

This code will also print the current date and the date after two months.

Output:

2024-01-14 16:31:53 +0000
2024-03-14 16:31:53 +0000

Adding Months, Days or Years to a Date

Take a look at this simple code snippet.

let monthsToAdd = 2
let daysToAdd = 1
let yearsToAdd = 1
let currentDate = Date()
 
var dateComponent = DateComponents()
 
dateComponent.month = monthsToAdd
dateComponent.day = daysToAdd
dateComponent.year = yearsToAdd
 
let futureDate = Calendar.current.date(byAdding: dateComponent, to: currentDate)
 
print(currentDate)
print(futureDate!)

Using the Date() function to get the current date, you can specify the number of months, days, and years to add with just a few variables. The magic happens with DateComponents, which neatly organizes these increments. Swift’s Calendar.current.date(byAdding:to:) method then effortlessly computes the future date for you.

Add Days, Months or Years to Current Date in Swift

Or you can do it this way:

import Foundation

let monthsToAdd = 2
let daysToAdd = 1

var newDate = Calendar.current.date(byAdding: .month, value: monthsToAdd, to: Date())

newDate = Calendar.current.date(byAdding: .day, value: daysToAdd, to: newDate!)

print(Date()) // Current date
print(newDate!)

This Swift code introduces another way to calculate a future date, by using the date(byAdding:value:to:) method from the Calendar class, you can easily add months and days to the current date.

In this example, I set the monthsToAdd and daysToAdd variables and then apply them sequentially to the current date using the date(byAdding:to:) method. The result is a new date, and I print it to the console.

Add Days, Months or Years to Current Date in Swift

Now, comparing it with the previous example, you’ll notice that this approach directly specifies the unit of time (month or day) within the date(byAdding:to:) method. It offers a more concise and arguably clearer syntax compared to manually setting components with DateComponents.

Adding Date and Time

You can use DateComponents and add hours, minutes, and seconds as well:

import Foundation

let hoursToAdd = 2
let minutesToAdd = 30
let secondsToAdd = 15

let currentDate = Date()

var dateComponent = DateComponents()

dateComponent.hour = hoursToAdd
dateComponent.minute = minutesToAdd
dateComponent.second = secondsToAdd

let futureDateTime = Calendar.current.date(byAdding: dateComponent, to: currentDate)

print(currentDate)
print(futureDateTime!)

In the above code example, you set the variables for hours, minutes, and seconds to add, and obtain the current date using Date(). With DateComponents, You specify the desired time interval. The future date is then calculated using Calendar.current.date(byAdding:to:).

Output:

2024-01-14 16:43:34 +0000
2024-01-14 19:13:49 +0000

Conclusion

Manipulating dates in Swift is straightforward thanks to the built-in Date()DateComponents(), and Calendar structures. These tools allow you to perform various operations such as adding or subtracting days, months, or years from the current date, as well as adding hours, minutes, and seconds to get a specific date and time.

Remember, when adding time to a date, you can simply extend the DateComponents object to include hours, minutes, and seconds. This makes your code cleaner and easier to read. Lastly, don’t forget to consider time zones when working with dates. The NSTimeZone.local property ensures that the date and time are adjusted according to the user’s local time zone.

For more Swift Code examples and tutorials, please check the Swift Code Examples page on this website.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *