为NSDate添加天数

时间:2011-11-17 09:42:55

标签: iphone objective-c ios nsdate

我想要添加几天的日期,我为此获得了许多代码但是我们下面没有显示任何代码是我的代码,请有人帮我修复此问题

int daysToAdd=[[appDlegateObj.selectedSkuData 
                                    objectForKey:@"Release Time"] intValue];

NSTimeInterval secondsForFollowOn=daysToAdd*24*60*60;

NSString *dateStr=[contentDic objectForKey:@"Date"];

NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSDate *dateFromString=[[NSDate alloc]init];

dateFromString=[dateFormatter dateFromString:dateStr];

[dateFormatter release];

 NSDate *date=[dateFromString dateByAddingTimeInterval:secondsForFollowOn];

9 个答案:

答案 0 :(得分:29)

// Initialize stringified date presentation
NSString *myStringDate = @"2011-11-17";

// How much day to add
int addDaysCount = 30;

// Creating and configuring date formatter instance
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

// Retrieve NSDate instance from stringified date presentation
NSDate *dateFromString = [dateFormatter dateFromString:myStringDate];

// Create and initialize date component instance
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:addDaysCount];

// Retrieve date with increased days count
NSDate *newDate = [[NSCalendar currentCalendar] 
                              dateByAddingComponents:dateComponents 
                                              toDate:dateFromString options:0];

NSLog(@"Original date: %@", [dateFormatter stringFromDate:dateFromString]);
NSLog(@"New date: %@", [dateFormatter stringFromDate:newDate]);

// Clean up
[dateComponents release], dateComponents = nil;
[dateFormatter release], dateFormatter = nil;

输出:

Original date: 2011-11-17
New date: 2011-12-17

答案 1 :(得分:8)

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

答案 2 :(得分:3)

您可以通过添加时间间隔

为NSDate添加天数
NSDate* newDate = [date dateWithTimeInterval:3600*24*numberOfDays sinceDate:otherDate];

虽然如果你想对它真正准确,并考虑到闰秒,白天节省时间和所有这些事情,你可能想要使用Date and Time Programming Guide和{{3中所述的NSDateComponents }}

答案 3 :(得分:3)

使用iOS 8和10.9,您现在可以更轻松地执行此操作。您可以在日期中添加任意数量的任何日历单位。我在NSDate上声明了一个非常小的类别方法,以使其更快(因为我只需要在我的应用程序中添加几天)。代码如下。

-(NSDate *)addDaysToDate:(NSNumber *)numOfDaysToAdd {
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:numOfDaysToAdd.integerValue toDate:self options:nil];
    return newDate;
}

关键词:NSCalendarUnitDay是我以前让它知道我想要添加几天的东西。您可以将其更改为其他枚举值,例如几个月或几年。

因为它是NSDate上的一个类别,所以我将值添加到self。如果您不想声明类别,则只需在方法参数中选择日期,例如:

-(NSDate *)addDays:(NSNumber *)numOfDaysToAdd toDate:(NSDate *)originalDate {
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:numOfDaysToAdd.integerValue toDate:originalDate options:nil];

    return newDate;
}

我使用NSNumber,但您可以轻松使用NSInteger。

最后,在Swift:

func addDaysToDate(daysToAdd: Int, originalDate: NSDate) -> NSDate? {
        let newDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.DayCalendarUnit, value: daysToAdd, toDate: originalDate, options: nil)
        return newDate
    }

如果你想摆脱NSDate?并且在展开可能的nil时感觉很舒服(添加日期可能会失败),您可以返回newDate!并摆脱?标记

答案 4 :(得分:2)

我建议您查看this文章以获得更清晰的解决方案。

答案 5 :(得分:2)

NSDateComponents *offsetComponents = [[NSDateComponents alloc] autorelease];
[offsetComponents setDay:1];

newDate = [[[CalendarContainer sharedCalendarContainer] gregorian] dateByAddingComponents:offsetComponents toDate:currentlyDisplayedDate options:0];

答案 6 :(得分:1)

在Swift 2.1.X和xcode 7.1 OSX 10.10.5中,您可以使用函数向前和向后添加任意天数

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

函数调用将当前日期递增9天

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

函数调用减去当前日期80天

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

答案 7 :(得分:0)

在Swift 2中,使用extension扩展NSDate:

extension NSDate {

    func addDays(days:Int) -> NSDate{
        let newDate = NSCalendar.currentCalendar().dateByAddingUnit(
            .Day,
            value: days,
            toDate: self,
            options: NSCalendarOptions(rawValue: 0))

        return newDate!
    }
}

答案 8 :(得分:-1)

    NSDate *Date=[datePicker date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd.MM.yy EEEE h:mm a"];