How do I add 1 day to an NSDate?

Objective CSwiftDateCocoa TouchNsdate

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

Swift 5.0 :

var dayComponent    = DateComponents()
dayComponent.day    = 1 // For removing one day (yesterday): -1
let theCalendar     = Calendar.current
let nextDate        = theCalendar.date(byAdding: dayComponent, to: Date())
print("nextDate : \(nextDate)")

Objective C :

NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
            
NSLog(@"nextDate: %@ ...", nextDate);

This should be self-explanatory.

Solution 2 - Objective C

Since iOS 8 you can use NSCalendar.dateByAddingUnit

Example in Swift 1.x:

let today = NSDate()
let tomorrow = NSCalendar.currentCalendar()
    .dateByAddingUnit(
         .CalendarUnitDay, 
         value: 1, 
         toDate: today, 
         options: NSCalendarOptions(0)
    )

Swift 2.0:

let today = NSDate()
let tomorrow = NSCalendar.currentCalendar()
    .dateByAddingUnit(
        .Day, 
        value: 1, 
        toDate: today, 
        options: []
    )

Swift 3.0:

let today = Date()
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)

Solution 3 - Objective C

Swift 5

let today = Date()
let nextDate = Calendar.current.date(byAdding: .day, value: 1, to: today)

Objective-C

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// now build a NSDate object for the next day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate: [NSDate date] options:0];

Solution 4 - Objective C

iOS 8+, OSX 10.9+, Objective-C

NSCalendar *cal = [NSCalendar currentCalendar];    
NSDate *tomorrow = [cal dateByAddingUnit:NSCalendarUnitDay 
                                   value:1 
                                  toDate:[NSDate date] 
                                 options:0];

Solution 5 - Objective C

A working Swift 3+ implementation based on highmaintenance's answer and vikingosegundo's comment. This Date extension also has additional options to change year, month and time:

extension Date {

    /// Returns a Date with the specified amount of components added to the one it is called with
    func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
        let components = DateComponents(year: years, month: months, day: days, hour: hours, minute: minutes, second: seconds)
        return Calendar.current.date(byAdding: components, to: self)
    }

    /// Returns a Date with the specified amount of components subtracted from the one it is called with
    func subtract(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
        return add(years: -years, months: -months, days: -days, hours: -hours, minutes: -minutes, seconds: -seconds)
    }
        
}

Usage for only adding a day as asked by OP would then be:

let today = Date() // date is then today for this example
let tomorrow = today.add(days: 1)

Solution 6 - Objective C

Swift 4.0 (same as Swift 3.0 in this wonderful answer just making it clear for rookies like me)

let today = Date()
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)

Solution 7 - Objective C

Update for Swift 4:

let now = Date() // the current date/time
let oneDayFromNow = Calendar.current.date(byAdding: .day, value: 1, to: now) // Tomorrow with same time of day as now

Solution 8 - Objective C

Use the below function and use days paramater to get the date daysAhead/daysBehind just pass parameter as positive for future date or negative for previous dates:

+ (NSDate *) getDate:(NSDate *)fromDate daysAhead:(NSUInteger)days
{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.day = days;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *previousDate = [calendar dateByAddingComponents:dateComponents
                                                     toDate:fromDate
                                                    options:0];
    [dateComponents release];
    return previousDate;
}

Solution 9 - Objective C

In swift

var dayComponenet = NSDateComponents()
dayComponenet.day = 1
    
var theCalendar = NSCalendar.currentCalendar()
var nextDate = theCalendar.dateByAddingComponents(dayComponenet, toDate: NSDate(), options: nil)
    

Solution 10 - Objective C

It works!

NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitDay;
NSInteger value = 1;
NSDate *today = [NSDate date];
NSDate *tomorrow = [calendar dateByAddingUnit:unit value:value toDate:today options:NSCalendarMatchStrictly];

Solution 11 - Objective C

Swift 3.0 very simple implementation would be:

func dateByAddingDays(inDays: Int) -> Date {
    let today = Date()
    return Calendar.current.date(byAdding: .day, value: inDays, to: today)!
}

Solution 12 - Objective C

Swift 4.0

extension Date {
    func add(_ unit: Calendar.Component, value: Int) -> Date? {
        return Calendar.current.date(byAdding: unit, value: value, to: self)
    }
}

Usage

date.add(.day, 3)!   // adds 3 days
date.add(.day, -14)!   // subtracts 14 days

Note: If you don't know why the lines of code end with an exclamation point, look up "Swift Optionals" on Google.

Solution 13 - Objective C

NSDate *today=[NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components=[[NSDateComponents alloc] init];
components.day=1;
NSDate *targetDate =[calendar dateByAddingComponents:components toDate:today options: 0];

Solution 14 - Objective C

NSDateComponents *dayComponent = [[[NSDateComponents alloc] init] autorelease];
dayComponent.day = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
dateToBeIncremented = [theCalendar dateByAddingComponents:dayComponent toDate:dateToBeIncremented options:0];

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.

In order to add a day I was doing the thing at the top, adding the:

NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];

Complicated, principally due to daylight saving.

Solution 15 - Objective C

In Swift 2.1.1 and xcode 7.1 OSX 10.10.5 ,you can add any number of days forward and backwards using function

func addDaystoGivenDate(baseDate:NSDate,NumberOfDaysToAdd:Int)->NSDate
{
    let dateComponents = NSDateComponents()
    let CurrentCalendar = NSCalendar.currentCalendar()
    let CalendarOption = NSCalendarOptions()

    dateComponents.day = NumberOfDaysToAdd

    let newDate = CurrentCalendar.dateByAddingComponents(dateComponents, toDate: baseDate, options: CalendarOption)
    return newDate!
}

function call for incrementing current date by 9 days

var newDate = addDaystoGivenDate(NSDate(), NumberOfDaysToAdd: 9)
print(newDate)

function call for decrement current date by 80 days

newDate = addDaystoGivenDate(NSDate(), NumberOfDaysToAdd: -80)
 print(newDate)

Solution 16 - Objective C

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

func addUnitToDate(unitType: NSCalendarUnit, number: Int, date:NSDate) -> NSDate {
    
    return NSCalendar.currentCalendar().dateByAddingUnit(
        unitType,
        value: number,
        toDate: date,
        options: NSCalendarOptions(rawValue: 0))!
    
}

print( addUnitToDate(.Day, number: 1, date: NSDate()) ) // Adds 1 Day To Current Date
print( addUnitToDate(.Hour, number: 1, date: NSDate()) ) // Adds 1 Hour To Current Date
print( addUnitToDate(.Minute, number: 1, date: NSDate()) ) // Adds 1 Minute To Current Date
    
// NOTE: You can use negative values to get backward values too

Solution 17 - Objective C

Simple Swift extensions for yesterday and tomorrow from any date:

extension Date {
    
    var previousDay: Date {
        Calendar.current.date(byAdding: DateComponents(day:-1), to: self)!
    }
    
    var nextDay: Date {
        Calendar.current.date(byAdding: DateComponents(day:+1), to: self)!
    }
    
}

I'm force unwrapping the optionals based on advice in the question here:
https://stackoverflow.com/questions/39358131/when-does-datebyaddingcomponentstodateoptions-return-nil

Solution 18 - Objective C

You can use NSDate's method - (id)dateByAddingTimeInterval:(NSTimeInterval)seconds where seconds would be 60 * 60 * 24 = 86400

Solution 19 - Objective C

In swift you can make extension to add method in NSDate

extension NSDate {
    func addNoOfDays(noOfDays:Int) -> NSDate! {
        let cal:NSCalendar = NSCalendar.currentCalendar()
        cal.timeZone = NSTimeZone(abbreviation: "UTC")!
        let comps:NSDateComponents = NSDateComponents()
        comps.day = noOfDays
        return cal.dateByAddingComponents(comps, toDate: self, options: nil)
    }
}

you can use this as

NSDate().addNoOfDays(3)

Solution 20 - Objective C

NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *tomorrowDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
    
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE, dd MMM yyyy"];
NSLog(@"%@", [dateFormatter stringFromDate:tomorrowDate]);

Solution 21 - Objective C

for swift 2.2:

let today = NSDate()
let tomorrow = NSCalendar.currentCalendar().dateByAddingUnit(
        .Day,
        value: 1,
        toDate: today,
        options: NSCalendarOptions.MatchStrictly)

Hope this helps someone!

Solution 22 - Objective C

NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
NSDate *startDate = [calendar dateFromComponents:components];
NSLog(@"StartDate = %@", startDate);

components.day += 1;
NSDate *endDate = [calendar dateFromComponents:components];
NSLog(@"EndDate = %@", endDate);

Solution 23 - Objective C

I had the same problem; use an extension for NSDate:

- (id)dateByAddingYears:(NSUInteger)years
				 months:(NSUInteger)months
				   days:(NSUInteger)days
				  hours:(NSUInteger)hours
				minutes:(NSUInteger)minutes
				seconds:(NSUInteger)seconds
{
	NSDateComponents * delta = [[[NSDateComponents alloc] init] autorelease];
	NSCalendar * gregorian = [[[NSCalendar alloc]
                               initWithCalendarIdentifier:NSCalendarIdentifierGregorian] autorelease];

	[delta setYear:years];
	[delta setMonth:months];
	[delta setDay:days];
	[delta setHour:hours];
	[delta setMinute:minutes];
	[delta setSecond:seconds];

	return [gregorian dateByAddingComponents:delta toDate:self options:0];
}

Solution 24 - Objective C

Swift 2.0

let today = NSDate()    
let calendar = NSCalendar.currentCalendar()
let tomorrow = calendar.dateByAddingUnit(.Day, value: 1, toDate: today, options: NSCalendarOptions.MatchFirst)

Solution 25 - Objective C

Swift 4, if all you really need is a 24 hour shift (606024 seconds) and not "1 calendar day"

Future: let dayAhead = Date(timeIntervalSinceNow: TimeInterval(86400.0))

Past: let dayAgo = Date(timeIntervalSinceNow: TimeInterval(-86400.0))

Solution 26 - Objective C

In Swift 4 or Swift 5, you can use like bellow:

let date = Date()
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: date)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let yesterday_date = dateFormatter.string(from: yesterday!)
print("yesterday->",yesterday_date)

output:

Current date: 2020-03-02
yesterday date: 2020-03-01

Solution 27 - Objective C

String extension: Convert String_Date > Date

extension String{
  func DateConvert(oldFormat:String)->Date{ // format example: yyyy-MM-dd HH:mm:ss 
    let isoDate = self
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
    dateFormatter.dateFormat = oldFormat
    return dateFormatter.date(from:isoDate)!
  }
}

Date extension: Convert Date > String

extension Date{
 func DateConvert(_ newFormat:String)-> String{
    let formatter = DateFormatter()
    formatter.dateFormat = newFormat
    return formatter.string(from: self)
 }
}

Date extension: Get +/- Date

extension String{
  func next(day:Int)->Date{
    var dayComponent    = DateComponents()
    dayComponent.day    = day
    let theCalendar     = Calendar.current
    let nextDate        = theCalendar.date(byAdding: dayComponent, to: Date())
    return nextDate!
  }

 func past(day:Int)->Date{
    var pastCount = day
    if(pastCount>0){
        pastCount = day * -1
    }
    var dayComponent    = DateComponents()
    dayComponent.day    = pastCount
    let theCalendar     = Calendar.current
    let nextDate        = theCalendar.date(byAdding: dayComponent, to: Date())
    return nextDate!
 }
}

Usage:

let today = Date()
let todayString = "2020-02-02 23:00:00"
let newDate = today.DateConvert("yyyy-MM-dd HH:mm:ss") //2020-02-02 23:00:00
let newToday = todayString.DateConvert(oldFormat: "yyyy-MM-dd HH:mm:ss")//2020-02-02
let newDatePlus = today.next(day: 1)//2020-02-03 23:00:00
let newDateMinus = today.past(day: 1)//2020-02-01 23:00:00

reference: from multiple question
How do I add 1 day to an NSDate?
math function to convert positive int to negative and negative to positive?
Converting NSString to NSDate (and back again)

Solution 28 - Objective C

update for swift 5

let nextDate = fromDate.addingTimeInterval(60*60*24)

Solution 29 - Objective C

Just for fun, with a few extensions and operator overloads, you can end up with something nice, like:

let today = Date()
let tomorrow = today + 1.days

, or

var date = Date()
date += 1.months

Below is the support code:

extension Calendar {
    struct ComponentWithValue {
        let component: Component
        let value: Int
    }
}

extension Int {
    var days: Calendar.ComponentWithValue {
        .init(component: .day, value: self)
    }
    
    var months: Calendar.ComponentWithValue {
        .init(component: .month, value: self)
    }
}

func +(_ date: Date, _ amount: Calendar.ComponentWithValue) -> Date {
    Calendar.current.date(byAdding: amount.component, value: amount.value, to: date)!
}

func +(_ amount: Calendar.ComponentWithValue, _ date: Date) -> Date {
    date + amount
}

func +=(_ date: inout Date, _ amount: Calendar.ComponentWithValue) {
    date = date + amount
}

The code is minimal, and can be easily extended to allow .months, .years, .hours, etc. Also support for subtraction (-) can be seamlessly added.

There is a forced unwrap, though, within the implementation of the + operator, however not sure under which circumstances can the calendar return a nil date.

Solution 30 - Objective C

Use following code:

NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];

As

addTimeInterval

is now deprecated.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - Objective CZaky GermanView Answer on Stackoverflow
Solution 2 - Objective CRob ZombieView Answer on Stackoverflow
Solution 3 - Objective CiHSView Answer on Stackoverflow
Solution 4 - Objective CvikingosegundoView Answer on Stackoverflow
Solution 5 - Objective CBenno KressView Answer on Stackoverflow
Solution 6 - Objective CJoshua DanceView Answer on Stackoverflow
Solution 7 - Objective CRyanView Answer on Stackoverflow
Solution 8 - Objective CBhupendraView Answer on Stackoverflow
Solution 9 - Objective CSteve NgView Answer on Stackoverflow
Solution 10 - Objective CDenZhukovView Answer on Stackoverflow
Solution 11 - Objective CAmJaView Answer on Stackoverflow
Solution 12 - Objective CquemefulView Answer on Stackoverflow
Solution 13 - Objective CRahul GuptaView Answer on Stackoverflow
Solution 14 - Objective CCarl HineView Answer on Stackoverflow
Solution 15 - Objective CDharmeshView Answer on Stackoverflow
Solution 16 - Objective Cn.by.nView Answer on Stackoverflow
Solution 17 - Objective CpkambView Answer on Stackoverflow
Solution 18 - Objective CTiagoView Answer on Stackoverflow
Solution 19 - Objective CMuhammad Aamir AliView Answer on Stackoverflow
Solution 20 - Objective CShrikant TanwadeView Answer on Stackoverflow
Solution 21 - Objective CPaul LehnView Answer on Stackoverflow
Solution 22 - Objective CVũ Ngọc GiangView Answer on Stackoverflow
Solution 23 - Objective CslashlosView Answer on Stackoverflow
Solution 24 - Objective CSébastien REMYView Answer on Stackoverflow
Solution 25 - Objective CJohn RobiView Answer on Stackoverflow
Solution 26 - Objective CEnamul HaqueView Answer on Stackoverflow
Solution 27 - Objective CMuhammad AsyrafView Answer on Stackoverflow
Solution 28 - Objective CManishView Answer on Stackoverflow
Solution 29 - Objective CCristikView Answer on Stackoverflow
Solution 30 - Objective CHemangView Answer on Stackoverflow