如何在NSDate中添加1天?

    技术2026-10-09  6

    本文翻译自:How do I add 1 day to an NSDate?

    Basically, as the title says. 基本上,正如标题所说。 I'm wondering how I could add 1 day to an NSDate . 我想知道如何在NSDate加1天。

    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.

    #1楼

    参考:https://stackoom.com/question/LGMT/如何在NSDate中添加-天


    #2楼

    Use following code: 使用以下代码:

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

    As 如

    addTimeInterval

    is now deprecated. 现已弃用。


    #3楼

    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: 使用以下函数并使用days paramater获取日期daysAhead / daysBehind只是将参数传递为将来日期的正数或以前日期的负数:

    + (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; }

    #4楼

    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. 但是,如果您使用它来添加一天到2013年3月31日,它将返回一个仅添加23小时的日期。 It may well actually have the 24, but using in calculations has only 23:00 hours added. 它实际上可能有24,但在计算中使用只增加了23:00小时。

    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. 同样,如果您向前推进至2013年10月28日,则代码会增加25小时,从而导致日期时间为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. 复杂,主要是由于夏令时。


    #5楼

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

    #6楼

    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);
    Processed: 0.016, SQL: 9