Modifying NSDate to represent 1 month from today

不打扰是莪最后的温柔 提交于 2019-12-09 04:08:46

问题


I'm adding repeating events to a Cocoa app I'm working on. I have repeat every day and week fine because I can define these mathematically (3600*24*7 = 1 week). I use the following code to modify the date:

[NSDate dateWithTimeIntervalSinceNow:(3600*24*7*(weeks))]

I know how many months have passed since the event was repeated but I can't figure out how to make an NSDate object that represents 1 month/3 months/6 months/9 months into the future. Ideally I want the user to say repeat monthly starting Oct. 14 and it will repeat the 14th of every month.


回答1:


(Almost the same as this question.)

From the documentation:

Use of NSCalendarDate strongly discouraged. It is not deprecated yet, however it may be in the next major OS release after Mac OS X v10.5. For calendrical calculations, you should use suitable combinations of NSCalendar, NSDate, and NSDateComponents, as described in Calendars in Dates and Times Programming Topics for Cocoa.

Following that advice:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = 1;
NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
[components release];

NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:nextMonth];

NSDateComponents *todayDayComponents = [gregorian components:NSDayCalendarUnit fromDate:today];

nextMonthComponents.day = todayDayComponents.day;
NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents];

[gregorian release];

There may be a more direct or efficient implementation, but this should be accurate and should point in the right direction.




回答2:


Use NSCalender, NSDateComponents and NSDate:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
components.month = 1;
NSDate *oneMonthFromNow = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:[NSDate date] options:0];

Just set the different properties of components to get different periods of time (e.g. 3 months, 6 months etc).

Note: Depending on the context, you may want to use [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease] instead of [NSCalendar currentCalendar], if by "1 month" you mean "1 month on the Gregorian calendar".




回答3:


I'll probably use NSCalendarDate.

Get current date's dayOfMonth, monthOfYear and yearOfCommonEra, as three numbers.

Add the required number of months (1/3/6/9), taking care of 1) whether that date exist (e.g. 31 April, 29 Feb on a non-leap year), and 2) roll-over of year.

Create that new date with NSCalendarDate's initWithYear:month:day:hour:minute:second:timeZone:




回答4:


NSCalendarDate is no longer supported by new SDK , will have problem while building for distribution.




回答5:


You can do like the below:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = 1;
NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
[components release];

NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nextMonth];
NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents];

[gregorian release]; 


来源:https://stackoverflow.com/questions/185780/modifying-nsdate-to-represent-1-month-from-today

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!