Basically, as the title says. I'm wondering how I could add 1 day to an NSDate
.
So if it were:
21st February 2011
It would become:
22nd February 2011
Or if it were:
31st December 2011
It would become:
1st January 2012.
Basically, as the title says. I'm wondering how I could add 1 day to an NSDate
.
So if it were:
21st February 2011
It would become:
22nd February 2011
Or if it were:
31st December 2011
It would become:
1st January 2012.
Here is a general purpose method which lets you add/subtract any type of unit(Year/Month/Day/Hour/Second etc) in the specified date.
Using Swift 2.2
Ok - I thought this was going to work for me. However, if you use it to add a day to the 31st March 2013, it'll return a date that has only 23 hours added to it. It may well actually have the 24, but using in calculations has only 23:00 hours added.
Similarly, if you blast forward to 28th Oct 2013, the code adds 25 hours resulting in a date time of 2013-10-28 01:00:00.
Very strange.
In order to add a day I was doing the thing at the top, adding the:
But this was a real mess for certain dates, principally due to daylight saving.
Objective-C, dates are a nightmare.
Since iOS 8 you can use
NSCalendar.dateByAddingUnit
Example in Swift 1.x:
Swift 2.0:
Swift 3.0:
Swift 3.0 very simple implemetation would be:
In swift you can make extension to add method in NSDate
you can use this as
Try this