如何在NSDate中添加1天?

本文翻译自: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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值