我看过这篇文章:iOS and finding Tomorrow。
提供的代码是:
units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* comps = [[NSCalendar currentCalendar] components:units fromDate:[NSDate date]];
// Add one day
comps.day = comps.day+1; // no worries: even if it is the end of the month it will wrap to the next month, see doc
// Recompose a new date, without any time information (so this will be at midnight)
NSDate* tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps];
问题:我需要在当前日期添加一天,例如,2013年6月21日到2013年5月21日之间的差异是1个月而不是0个月。
我正在使用的代码:
NSDate *selectedDate = [picker2 date];
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *conversionInfo = [currCalendar components:unitFlags fromDate:selectedDate toDate:[NSDate date] options:0];
DLog(@"%i",conversionInfo.month);
DLog(@"%i",conversionInfo.day);
conversionInfo.day +=1;
DLog(@"%i",conversionInfo.day);
DLog(@"%i",conversionInfo.month);
int months = [conversionInfo month];
但是当我试图在2013年6月21日到2013年5月21日之间找到区别时 - >仍然会给我0个月而不是1个月。
需要一些帮助。
答案 0 :(得分:15)
形成一个日期组件,其中包含您要添加到原始日期的天数。通过将此组件添加到原始日期,从当前日历形成日期。
NSDateComponents *dateComponents = [NSDateComponents new];
dateComponents.day = 1;
NSDate *newDate = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents
toDate: selectedDate
options:0];
答案 1 :(得分:8)
这是我使用的方法:
+ (NSDate *)addDays:(NSInteger)days toDate:(NSDate *)originalDate {
NSDateComponents *components= [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *calendar = [NSCalendar currentCalendar];
return [calendar dateByAddingComponents:components toDate:originalDate options:0];
}
有了它,您可以根据需要添加任意天数。它也适用于负数,因此您可以减去天数。
答案 2 :(得分:3)
简单直接的解决方案:
NSDate *currentDate = [NSDate date];
NSDate *nextDate = [currentDate dateByAddingTimeInterval:(24*3600)];
这对我来说很好。
答案 3 :(得分:1)
对于快速的googlers:
NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: 1, toDate: date, options: nil)!
答案 4 :(得分:0)
首先,您要为NSDateComponent分配日期而不是月份。 执行以下代码,找出inDate和newDate之间的区别
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:inDate];
NSInteger monthFlag = 1; components.month = minuteFlag;
// Retrieve date with increased days count
NSDate *newDate = [[NSCalendar currentCalendar]
dateByAddingComponents:components
toDate:monthFlag
options:0];
答案 5 :(得分:0)
要在日期中添加任意数量的任何组件,例如:2天或2个月,1年,请执行以下操作:
Calendar.current.date(byAdding: DateComponents(month: -1), to: date)! //this create date with previous month for date.
您可以使用任何内容初始化DateComponents
:
public init(calendar: Calendar? = default, timeZone: TimeZone? = default, era: Int? = default, year: Int? = default, month: Int? = default, day: Int? = default, hour: Int? = default, minute: Int? = default, second: Int? = default, nanosecond: Int? = default, weekday: Int? = default, weekdayOrdinal: Int? = default, quarter: Int? = default, weekOfMonth: Int? = default, weekOfYear: Int? = default, yearForWeekOfYear: Int? = default)