- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I add 1 day to an NSDate?
In this article, you will learn how to add the number of days to a date in the Swift language.
Many times, you might need to modify the date by changing some number of days, weeks, months, etc. Swift provides an in-built concept for adding a component to date. Let's see some examples.
Let's take a brief look at a few of the common concepts that will be used in these examples.
Calendar Class
The Calendar class in Swift is a class that represents a calendar, which is a system for organizing dates. It is used to perform calendar-related calculations, such as determining the number of days in a month or the first day of the week.
DateFormatter Class
The DateFormatter class in Swift is a class that is used to convert between dates and strings. It can be used to parse a date from a string or to format a date as a string.
How to add a day to the custom date?
Example 1
Algorithm
Step 1 - Get the current calendar reference object
Step 2 - Create an object of the DateFormatter class
Step 3 - Set the timezone
Step 4 - Create an input date string
Step 5 - Set the date format according to the date string
Step 6 - Convert a date string into a date object
Step 7 - Add the day component with the number of days' value
Example
import Foundation let calendar = Calendar.current let dateFormatter = DateFormatter() dateFormatter.timeZone = .init(abbreviation: "UTC") let dateString1 = "08-10-2022" dateFormatter.dateFormat = "dd-MM-yyyy" if let dateObject = dateFormatter.date(from: dateString1), let dateByAddedDay = calendar.date(byAdding: .day, value: 1, to: dateObject) { print("the target date: \(dateObject) and date after adding one day: \(dateByAddedDay)") }
Output
the target date: 2022-10-08 00:00:00 +0000 and date after adding one day: 2022-10-09 00:00:00 +0000
Example 2
import Foundation let calendar = Calendar.current let dateFormatter = DateFormatter() dateFormatter.timeZone = .init(abbreviation: "UTC") let dateString2 = "12/23/2022" dateFormatter.dateFormat = "MM/dd/yyyy" if let dateObject = dateFormatter.date(from: dateString2), let dateByAddedDay = calendar.date(byAdding: .day, value: 1, to: dateObject) { print("the target date: \(dateObject) and date after adding one day: \(dateByAddedDay)") }
Output
the target date: 2022-12-23 00:00:00 +0000 and date after adding one day: 2022-12-24 00:00:00 +0000
Explanation
We changed the date format in the example above and then added the days.
How to add a day to the current date?
import Foundation let calendar = Calendar.current let dateFormatter = DateFormatter() dateFormatter.timeZone = .init(abbreviation: "UTC") let targetDate = Date() if let dateByAddedDay = calendar.date(byAdding: .day, value: 1, to: targetDate) { print("the target date: \(targetDate) and date after adding one day: \(dateByAddedDay)") }
Output
the target date: 2023-01-06 16:49:29 +0000 and date after adding one day: 2023-01-07 16:49:29 +0000
Note here, the output may vary depending on the date on which you are learning this.
How to add other components to a date?
In a similar way, you can add other date components like a week, month, year, etc.
Example
import Foundation let calendar = Calendar.current let dateFormatter = DateFormatter() dateFormatter.timeZone = .init(abbreviation: "UTC") let dateString = "08-10-2022" dateFormatter.dateFormat = "dd-MM-yyyy" if let dateObject = dateFormatter.date(from: dateString) { print("The target date: \(dateObject)") if let dateByAddedDays = calendar.date(byAdding: .day, value: 7, to: dateObject) { print("Next Week: \(dateByAddedDays)") } if let dateByAddedMonth = calendar.date(byAdding: .month, value: 1, to: dateObject) { print("Next Month: \(dateByAddedMonth)") } if let dateByAddedYear = calendar.date(byAdding: .year, value: 1, to: dateObject) { print("Next Year: \(dateByAddedYear)") } }
Output
The target date: 2022-10-08 00:00:00 +0000 Next Week: 2022-10-15 00:00:00 +0000 Next Month: 2022-11-08 00:00:00 +0000 Next Year: 2023-10-08 00:00:00 +0000
Explanation
In the above example, you converted the date to get the next week, month, and year.
Conclusion
The Calendar and DateFormatter classes can be used to manipulate dates by adding different date components. You can modify the custom dates and the current date.