热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将NSDate修改为一个月后-ModifyingNSDatetorepresent1monthfromtoday

ImaddingrepeatingeventstoaCocoaappImworkingon.Ihaverepeateverydayandweekfinebec

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:

我正在为一个Cocoa应用程序添加重复事件。我每天和每周都在重复,因为我可以用数学方法来定义(3600*24*7 = 1周)。我使用以下代码修改日期:

[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.

我知道事件重复了多少个月,但我不知道如何制作一个NSDate对象,它表示未来1个月/3个月/6个月/9个月。最理想的情况是,我希望用户每月从10月14日开始重复每月的第14次。

5 个解决方案

#1


37  

(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.

强烈反对使用NSCalendarDate。它还没有被弃用,但是它可能在Mac OS X v10.5之后的下一个主要OS版本中。对于calendrical计算,您应该使用合适的NSCalendar、NSDate和NSDateComponents的组合,如在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


22  

Use NSCalender, NSDateComponents and NSDate:

使用NSCalender, NSDateComponents和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).

只需设置组件的不同属性以获得不同的时间段(例如3个月、6个月等)。

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".

注意:根据上下文的不同,您可能想要使用[[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease而不是[NSCalendar currentCalendar],如果“1个月”指的是“公历上的1个月”。

#3


0  

I'll probably use NSCalendarDate.

我可能会使用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.

添加所需的月数(1/3/6/9),注意1)该日期是否存在(例如:4月31日,非闰年2月29日),以及2)年滚转。

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

用NSCalendarDate的initWithYear:month:day:hour:minute:second:时区:

#4


0  

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

新的SDK不再支持NSCalendarDate,在为发行版构建时将出现问题。

#5


0  

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

推荐阅读
author-avatar
729453686_5be5b9
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有