基本上,就像标题说的那样。我想知道如何添加1天的NSDate。

如果它是:

21st February 2011

它将变成:

22nd February 2011

或者如果它是:

31st December 2011

它将变成:

1st January 2012.

当前回答

这是一个通用的方法,它允许您在指定的日期中添加/减去任何类型的单位(年/月/日/小时/秒等)。

使用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

其他回答

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)")

目标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);

这应该是不言而喻的。

简单的Swift扩展昨天和明天从任何日期:

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)!
    }
    
}

我在这里根据问题中的建议强制展开可选选项: 何时dateByAddingComponents:toDate:options返回nil?

只是为了好玩,通过一些扩展和操作符重载,你可以得到一些不错的东西,比如:

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

, or

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

下面是支持代码:

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
}

代码是最少的,并且可以很容易地扩展到允许.月,.年,.小时等。还可以无缝添加对减法(-)的支持。

虽然在+操作符的实现中有一个强制的展开,但是不确定在哪种情况下日历可以返回nil日期。

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);

对于swift 2.2:

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

希望这能帮助到一些人!