- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 to subtract 1 hour from current time using Swift?
To subtract hours from a date in swift we need to create a date first. Once that date is created we have to subtract hours from that, though swift does not provide a way to subtract date or time, but it provides us a way to add date or date component in negative value. In this example we’ll see how we can achieve the same.
Let’s create a date first, let it be today,
let today = Date()
Now to modify this date we’ll use the add function with negative value,
let modifiedDate = Calendar.current.date(byAdding: .hour, value: -2, to: today)!
Now to see the difference between both the dates, let’s add print statement for both of these. Our complete code should now look like this.
let today = Date() print(today) let modifiedDate = Calendar.current.date(byAdding: .hour, value: -2, to: today)! print(modifiedDate)
When we run the above code on a playground simulator we get the following result, which shows exactly a difference of two hours in both the date, and the modified date.
- Java Program to subtract hours from current time using Calendar.add() method
- Subtract seconds from current time using Calendar.add() method in Java
- Subtract minutes from current time using Calendar.add() method in Java
- Insert current time minus 1 hour to already inserted date-time records in MYSQL
- Subtract days from current date using Calendar.DATE in Java
- Java Program to subtract months from current date using Calendar.add() method
- Java Program to subtract year from current date
- Java Program to subtract week from current date
- How to subtract 30 days from the current datetime in MySQL?
- How to subtract 10 days from the current datetime in MySQL?
- Swift Program to Subtract Two Matrix Using Multi-dimensional Arrays
- Python program to convert time from 12 hour to 24 hour format
- C++ program to convert time from 12 hour to 24 hour format
- C# program to convert time from 12 hour to 24 hour format
- How to convert 12-hour time scale to 24-hour time in R?