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

如果它是:

21st February 2011

它将变成:

22nd February 2011

或者如果它是:

31st December 2011

它将变成:

1st January 2012.

当前回答

基于highmaintenance的回答和vikingosegundo的评论,一个工作的Swift 3+实现。这个日期扩展也有额外的选项来改变年,月和时间:

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

}

如果按照OP的要求只增加一天,则用法如下:

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

其他回答

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

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日期。

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

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

好吧,我以为这对我有用。但是,如果您使用它在2013年3月31日之后添加一天,它将返回一个只添加了23小时的日期。它实际上可能有24小时,但在计算中只添加了23:00小时。

类似地,如果您向前扩展到2013年10月28日,代码将增加25小时,从而导致日期时间为2013-10-28 01:00:00。

为了添加一天,我正在做的事情在顶部,添加:

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

复杂,主要是由于夏令时。

Swift 4,如果你真正需要的是24小时轮班(60*60*24秒)而不是“1个日历天”

未来: let dayAhead = Date(timeIntervalSinceNow: TimeInterval(86400.0))

过去: let dayAgo = Date(timeIntervalSinceNow: TimeInterval(-86400.0))

在swift中,您可以在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)
    }
}

你可以用这个

NSDate().addNoOfDays(3)

我也有同样的问题;使用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];
}