在一天之后和之前一天的选择目标-c之前添加当前日期

3 objective-c datepicker nsdate ios

我有一个日期选择器,我想只有一行有3个选项:当天,前一天和前一天.

你能帮帮我吗?

我的问题是,如何在一天后和前一天添加"当天"?

and*_*azz 5

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *end = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
Run Code Online (Sandbox Code Playgroud)

其中days是一个整数,表示您要从当天添加/减去的天数.

  • 这是[正确](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW3)的方式这样做. (3认同)